通过字符串迭代 [英] Iterating through a string

查看:97
本文介绍了通过字符串迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个字符串,其中包含以空格分隔的数据元素。我还

有一个函数,它返回给定数量空格的字符串开头的字符数。我正在使用带有strchr的循环来获得空间数量,而我只是想知道是否有更高效的实现这种方式的b $ b(即更快)方式。有什么想法吗?当前功能代码

如下:


int GetNewPosition(const std :: string& DataStr,const int Spaces)

{

int i = 0; //空间计数器

char * b = const_cast< char *>(DataStr.c_str()); //字符串的开头


do {

b = strchr(b,''''); //找到下一个空间

++ i; ++ B; //增加空间计数器和指针

} while(i< Spaces&& b!= NULL);


如果(b)返回b - DataStr.c_str();

否则返回-1;

}

感谢任何帮助,

谢谢阅读,

史蒂夫。

解决方案

2004年6月8日星期二22:42:36 + 0100 lang.c ++,史蒂夫

< st ******* @ hotmail.com>写了,

给定数量的空格的字符串。我正在使用带有strchr的循环来获取空格的数量,我只是想知道是否有更高效的(即更快)实现此方法。




为了提高效率,避免不止一次超过同一个地面,即

从前一个位置继续,并且不要在开始时重启

of字符串。


使用string :: find()函数而不是.c_str()并下降到

低级旧库strchr()。


不要从另一个中减去一个.c_str()。无法保证

..c_str()每次都指向同一个缓冲区。


看看使用字符串拆分器功能是否更适合你?
http://groups.google.com/gr*********....earthlink.net


可能有一个标准的库输入流类,你可以使用它来b / b $ b来吸取你想要的数据。

如果你想自己动手,你可以创建一个专门的迭代器对象

,它接受你的字符串并让你迭代它。当你想要下一个位置时,似乎你总是从头开始,并且通过字符串计算
来查找下一个位置。通过使用一个对象来包装

字符串,你可以在对象中维护这个状态信息,并且不需要
需要计算每次都是从头开始。

class FooBar

{

public:

FooBar(const std :: string& ; DataStr)

:_DataStr(DataStr),_ position(0)

{

//移动位置指向第一个数据项。

}


std :: string GetDataItem()const

{

//返回表示下一个数据项的字符串。

}

bool IsValid()const; //如果还有更多项目要返回true,

否则为false。

bool Next(); //通过数据字符串前进到字符串结尾或下一个

找到数据项,

//更新位置计数器。


bool First(); //回到第一个数据项的位置,所以我们

可以再次迭代

//字符串。


private:

const std :: string _DataStr

int _position;


};

.......


FooBar myfoobar(DataStr);


for(myfoobar.First(); myfoobar.IsValid(DataItem); myfoobar.Next())

{

string ds = myfoobar.GetDataItem();


}


" Steve" < ST ******* @ hotmail.com>在消息中写道

news:Ohrxc.1911


ud5.1728@newsfe4-gui ...

大家好,

我有一个字符串,其中包含用空格分隔的数据元素。我还有一个函数,它返回给定数量空格的字符串开头
的字符数。我正在使用带有strchr的循环来获取空格的数量,我只是想知道是否有更多的
有效(即更快)的方法来实现这一点。有什么想法吗?当前功能代码
如下:

int GetNewPosition(const std :: string& DataStr,const int Spaces)
{
int i = 0; //空间计数器
char * b = const_cast< char *>(DataStr.c_str()); //字符串的开头

做{
b = strchr(b,''''); //找到下一个空间
++ i; ++ B; //增加空间计数器和指针
} while(i< Spaces&& b!= NULL);

如果(b)返回b - DataStr.c_str();
否则返回-1;
}

感谢任何帮助,
感谢阅读,
史蒂夫。


Hi Guys,

I have a string which contains data elements separated by spaces. I also
have a function which returns the number of characters from the beginning of
the string for a given number of spaces. I am using a loop with strchr for
the number of spaces, and I was just wondering if there was a more efficient
(ie faster) way of achieving this. Any ideas please? Current function code
below:

int GetNewPosition(const std::string& DataStr, const int Spaces)
{
int i = 0; // space counter
char* b = const_cast<char*>(DataStr.c_str()); // beginning of string

do {
b = strchr(b, '' ''); // find next space
++i; ++b; // increment space counter and pointer
} while(i<Spaces && b != NULL);

if(b) return b - DataStr.c_str();
else return -1;
}
Any help is appreciated,
Thanks for reading,
Steve.

解决方案

On Tue, 8 Jun 2004 22:42:36 +0100 in comp.lang.c++, "Steve"
<st*******@hotmail.com> wrote,

the string for a given number of spaces. I am using a loop with strchr for
the number of spaces, and I was just wondering if there was a more efficient
(ie faster) way of achieving this.



For efficiency, avoid going over the same ground more than once, i.e.
continue from the previous position and do not restart at the beginning
of the string.

Use string::find() functions rather than .c_str() and dropping down to
low-level old library strchr().

Do not subtract one .c_str() from another. There is no guarantee that
..c_str() will point to the same buffer every time.

See if using a string splitter function would suit you better?
http://groups.google.com/gr*********....earthlink.net


There''s probably a standard library input stream class which you can use to
suck out the pieces of data you want.
If you want to roll your own, you could create a specialized iterator object
which takes your string and lets you iterate over it. It seems that you
are always starting from the beginning when you want the next position and
count through the string to find the next one. By using an object to wrap
the
string, you could maintain this state information in the object and wouldn''t
need to compute it from scratch every time.
class FooBar
{
public:
FooBar( const std::string& DataStr )
:_DataStr(DataStr), _position(0)
{
// move position to point to the first data item.
}

std::string GetDataItem( ) const
{
// return a string representing the next data item.
}
bool IsValid() const ; // return true if there are more items to return,
false otherwise.
bool Next(); // advance through datastring until end of string or next
data item is found,
// update position counter.

bool First() ; // wind back position to be at the first data item, so we
can iterate again over
// the string.

private:
const std::string _DataStr
int _position;

};

.......

FooBar myfoobar( DataStr);

for ( myfoobar.First() ; myfoobar.IsValid( DataItem); myfoobar.Next() )
{
string ds = myfoobar.GetDataItem();

}

"Steve" <st*******@hotmail.com> wrote in message
news:Ohrxc.1911


ud5.1728@newsfe4-gui...

Hi Guys,

I have a string which contains data elements separated by spaces. I also
have a function which returns the number of characters from the beginning of the string for a given number of spaces. I am using a loop with strchr for
the number of spaces, and I was just wondering if there was a more efficient (ie faster) way of achieving this. Any ideas please? Current function code
below:

int GetNewPosition(const std::string& DataStr, const int Spaces)
{
int i = 0; // space counter
char* b = const_cast<char*>(DataStr.c_str()); // beginning of string

do {
b = strchr(b, '' ''); // find next space
++i; ++b; // increment space counter and pointer
} while(i<Spaces && b != NULL);

if(b) return b - DataStr.c_str();
else return -1;
}
Any help is appreciated,
Thanks for reading,
Steve.



这篇关于通过字符串迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆