快速std :: string问题 [英] Quick std::string question

查看:72
本文介绍了快速std :: string问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个std :: string,我想用>替换所有'','

字符或,,即A,B,C。 - > A或B或C。

遵循最佳方式吗?


int idx;

while((idx = str.find_first_of( '',''))> = 0){

str.replace(idx,1,"");

str.insert(idx," ;或者是;);

}


(不幸的是,Stroustrup在这里并没有太多启发。)


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

Let''s say I have a std::string, and I want to replace all the '',''
characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the
following the best way to do it?

int idx;
while( (idx=str.find_first_of('','')) >= 0 ) {
str.replace( idx, 1, "" );
str.insert( idx, " or " );
}

(Stroustrup wasn''t too illuminating here, unfortunately.)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

2004年2月9日星期一21:18:47 +0000(UTC),Christopher Benson-Manica

< at *** @ nospam.cyberspace.org>写道:
On Mon, 9 Feb 2004 21:18:47 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
让我们说我有一个std :: string,我想用''替换所有'',''
字符或,,即A,B,C。 - > A或B或C。
是否遵循最佳方式?

int idx;
while((idx = str.find_first_of('',''))> = 0) {
str.replace(idx,1,"");
str.insert(idx,"或");
}

(不幸的是,Stroustrup在这里并没有太多启发。)
Let''s say I have a std::string, and I want to replace all the '',''
characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the
following the best way to do it?

int idx;
while( (idx=str.find_first_of('','')) >= 0 ) {
str.replace( idx, 1, "" );
str.insert( idx, " or " );
}

(Stroustrup wasn''t too illuminating here, unfortunately.)




看看这是否适合你:


// /

///将字符串中子字符串的所有出现替换为另一个

///字符串,就地。

// /

/// @param s要替换的字符串。将被修改。

/// @param sub要替换的子字符串。

/// @param用其他字符串替换子字符串。

///

/// @return替换后的字符串。

inline std :: string&

replacein(std :: string& s,const std :: string& sub,

const std :: string& other)

{

断言(!sub.empty());

size_t b = 0;

for( ;;)

{

b = s.find(sub,b);

if(b == s.npos)break;

s.replace(b, sub.size(),other);

b + = other.size();

}

返回s;

}


-

见到你。



See if this will work for you:

///
/// Replace all occurences of a substring in a string with another
/// string, in-place.
///
/// @param s String to replace in. Will be modified.
/// @param sub Substring to replace.
/// @param other String to replace substring with.
///
/// @return The string after replacements.
inline std::string &
replacein(std::string &s, const std::string &sub,
const std::string &other)
{
assert(!sub.empty());
size_t b = 0;
for (;;)
{
b = s.find(sub, b);
if (b == s.npos) break;
s.replace(b, sub.size(), other);
b += other.size();
}
return s;
}

--
Be seeing you.


Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
让我们说我有一个std :: string,我想用''替换所有'',''/
字符。或,,即A,B,C。 - > A或B或C。
是否遵循最佳方式?

int idx;
while((idx = str.find_first_of('',''))> = 0) {


这将从头开始,每个'',''被找到并再次

搜索已经搜索过的部分。我将以下两行代替




int idx = 0;

while((idx = str) .find_first_of('','',idx))> = 0){


你甚至可以通过跳过替换文本来节省更多,
$ b也是$ b。

str.replace(idx,1,"");
str.insert(idx,"或);



为什么你先用空字符串替换'',''然后插入

或而不是直接用或?


str.replace(idx,1,"或);

}

(Stroustrup不幸的是,这里并没有太多启发。)
Let''s say I have a std::string, and I want to replace all the '',''
characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the
following the best way to do it?

int idx;
while( (idx=str.find_first_of('','')) >= 0 ) {
This will start from the beginning for each '','' that was found and again
search the already searched parts. I''d replace the above two lines
with:

int idx = 0;
while( (idx=str.find_first_of('','', idx)) >= 0 ) {

You could even save a bit more by jumping over the replacement text,
too.
str.replace( idx, 1, "" );
str.insert( idx, " or " );

Why do you first replace the '','' with an empty string and then insert
the " or " instead of just replacing it directly with " or "?

str.replace(idx, 1, " or ");
}

(Stroustrup wasn''t too illuminating here, unfortunately.)




-

从来没有想过为什么胡萝卜比橙子更橙?? br />



--
Never wondered why a carrot is more orange than an orange?


" Christopher Benson-Manica" <在*** @ nospam.cyberspace.org>在消息中写道

新闻:c0 ********** @ chessie.cirr.com ...
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c0**********@chessie.cirr.com...
让我们说我有一个标准:: string,我想用''替换所有'',''
字符或,,即A,B,C。 - > A或B或C。
是否遵循最佳方式?

int idx;
while((idx = str.find_first_of('',''))> = 0) {
str.replace(idx,1,"");
str.insert(idx,"或");
}

(不幸的是,Stroustrup在这里并没有太多启发。)
Let''s say I have a std::string, and I want to replace all the '',''
characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the
following the best way to do it?

int idx;
while( (idx=str.find_first_of('','')) >= 0 ) {
str.replace( idx, 1, "" );
str.insert( idx, " or " );
}

(Stroustrup wasn''t too illuminating here, unfortunately.)




为了更多功能,我已经改变了你的''角色以找到''

to''string to find'':

#include< iostream>

#include< string>


/ *如果''来自''匹配''''或''来自''为空,* /

/ *不解析's'',则返回std :: string :: npos * /

/ * * /

/ *否则返回已完成的替换次数* /

/ * * /


std :: string :: size_type repl(std :: string& s,

co nst std :: string& from,

const std :: string& to)

{

std :: string :: size_type cnt( std :: string :: npos);


if(from!= to&& !from.empty())

{

std :: string :: size_type pos1(0);

std :: string :: size_type pos2(0);

const std :: string :: size_type from_len(from.size());

const std :: string :: size_type to_len(to .size());

cnt = 0;


while((pos1 = s.find(from,pos2))!= std :: string :: npos)

{

s.replace(pos1,from_len,to);

pos2 = pos1 + to_len;

++ cnt;

}

}


返回cnt;

}


int main()

{

std :: string s(" A,B,C");


const std :: string old_seq(",");

const std :: string new_seq(" or);


std :: cout<< 原始字符串:\ n"

<< \ n \ nn;


const std :: string :: size_type count(repl(s,old_seq,new_seq));

const std :: string dq(count> 0,''"'');

const bool解析(count!= std :: string :: npos);

const bool found(解析&&> 0);


if(parsed)

{

std :: cout<< count

<< "序列的出现\"" << old_seq<< " \""

<< (count?" replace with sequence":find)

<< dq<< (count?new_seq:"")<< dq

<< \ n \ nn;

}

其他

std :: cout<< 没什么可改变的,但没有什么可改变的;


std :: cout<< (解析&&发现

?std :: string(" New string:\ n" + dq + s + dq)

:没有变化制作了)

<< ''\ n'';


返回0;

}


原始字符串:

A,B,C


2出现序列,用序列替换或


新字符串:

A或B或C


HTH,

-Mike



For more versatility, I''ve changed your ''character to find''
to ''string to find'':
#include <iostream>
#include <string>

/* If ''from'' matches ''to'' or ''from'' is empty, */
/* does not parse ''s'', returns std::string::npos */
/* */
/* Otherwise returns number of replacements done */
/* */

std::string::size_type repl(std::string& s,
const std::string& from,
const std::string& to)
{
std::string::size_type cnt(std::string::npos);

if(from != to && !from.empty())
{
std::string::size_type pos1(0);
std::string::size_type pos2(0);
const std::string::size_type from_len(from.size());
const std::string::size_type to_len(to.size());
cnt = 0;

while((pos1 = s.find(from, pos2)) != std::string::npos)
{
s.replace(pos1, from_len, to);
pos2 = pos1 + to_len;
++cnt;
}
}

return cnt;
}

int main()
{
std::string s("A,B,C");

const std::string old_seq(",");
const std::string new_seq(" or ");

std::cout << "Original string:\n"
<< ''"'' << s << ''"''
<< "\n\n";

const std::string::size_type count(repl(s, old_seq, new_seq));
const std::string dq(count > 0, ''"'');
const bool parsed(count != std::string::npos);
const bool found(parsed && count > 0);

if(parsed)
{
std::cout << count
<< " occurences of sequence \"" << old_seq << "\""
<< (count ? " replaced with sequence " : " found")
<< dq << (count ? new_seq : "") << dq
<< "\n\n";
}
else
std::cout << "Nothing to change\n\n";

std::cout << (parsed && found
? std::string("New string:\n" + dq + s + dq)
: "No changes made")
<< ''\n'';

return 0;
}

Original string:
"A,B,C"

2 occurences of sequence "," replaced with sequence " or "

New string:
"A or B or C"

HTH,
-Mike


这篇关于快速std :: string问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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