std :: string :: npos always< std :: string :: size()? [英] std::string::npos always < std::string::size() ?

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

问题描述

std :: string :: npos总是小于任何std :: string的大小()吗?


我试图处理所有的替换出现一个substr,其中
替换也包含substr。益。我能想到的就是:


#include< string>


int main()

{

std :: string text;

text =" \ n这是一个带有换行符的测试行\ n< -here和\ n< -here和

\ n \ n< -two here";


//格式化文本

std :: string formattedText (文本);


//更改\ n的每个出现次数to\\\\ n>

std :: string :: size_type index = 0;


do

{

index = formattedText.find(''\ n'',index);

if(index!= std :: string :: npos)

{

formattedText.insert(index," \ r");

index + = 2;

}

} while(index< formattedText.size());


//在这里调试formattedText的内容


返回0;

}


但是,它取决于std :: string :: npos总是小于
$的大小b $ b字符串,我不确定这是否安全。

解决方案

1月10日下午4:52,克里斯托弗Pisz的" < some ... @ somewhere.netwrote:


std :: string :: npos总是小于任何std :: string的大小()?


我正在尝试替换一个substr的所有出现,其中

替换也包含substr。益。我能想到的就是:


#include< string>


int main()

{

std :: string text;

text =" \ n这是一个带有换行符的测试行\ n< -here和\ n< -here和

\ n \ n< -two here";


//格式化文本

std :: string formattedText (文本);


//更改\ n的每个出现次数to\\\\ n>

std :: string :: size_type index = 0;


do

{

index = formattedText.find(''\ n'',index);

if(index!= std :: string :: npos)

{

formattedText.insert(index," \ r");

index + = 2;

}

} while(index< formattedText.size());


//在这里调试formattedText的内容


返回0;


}


但是,它取决于std :: string :: npos总是小于

字符串,我不确定这是否安全。



我知道这是一个可怕的解决方案,但是沿着

行将某些字符串的内容复制到另一个字符串呢? br />
int i;


for(i = 0; i< string1.size(); i ++)

{

if(string1 [i] ==''\ n'')string2 + =" \r\\\
" ;;

else string2 + = string1 [i ];

}

这是一种缓慢而臃肿的工作方式。


1月10日,8:52 * am,Christopher Pisz, < some ... @ somewhere.netwrote:


std :: string :: npos总是小于任何std :: string的大小()?


我正在尝试替换一个substr的所有出现,其中

替换也包含substr。益。我能想到的就是:


#include< string>


int main()

{

* * std :: string text;

* * text =" \ n这是一个带有换行符的测试行\ n< -here和\\\
< ; -here和

\ n \\ nn< -two here";


* * //格式化文本

* * std :: string formattedText(text);


* * //更改\ n的每个出现次数到\\\\ n>

* * std :: string :: size_type index = 0;


* * do

* * {

* * * index = formattedText.find(''\ n'',index);

* * * if(index! = std :: string :: npos)

* * * {

* * * * * formattedText.insert(index," \r");

* * * * * index + = 2;

* * *}

* *} while(index< formattedText.size()) ;


* * //这里调试formattedText的内容


* *返回0;


}


但是,它取决于std :: string :: npos总是小于

字符串的大小,我不确定是否安全与否。



没关系/安全。一个std :: string不能有一个length()(因为它比std :: string :: npos更好地是
std :: string,size())。所以,你的代码

应该是完全没问题的,当所有

实例的''\ n''被处理完毕后,它总会退出。 br />


1月10日,1:29 * pm,Abhishek Padmanabh< abhishek.padman ... @ gmail.com>

写道:


1月10日,8:52 * am,Christopher Pisz < some ... @ somewhere.netwrote:



std :: string :: npos总是小于任何std :: string'的大小()?


我正在尝试替换一个substr的所有出现,其中

替换也包含substr 。益。我只能提出:


#include< string>


int main()

{

* * std :: string text;

* * text =" \ n这是一个带有换行符的测试行\ n< -here和\\\
< -here和

\ n \ n< ; -two here;


* * //格式化文本

* * std :: string formattedText(text);


* * //更改\ n的每个出现次数到\\\\ n&#;>
* * std :: string :: size_type index = 0;


* * do

* * {

* * * index = formattedText.find (''\ n'',索引);

* * * if(index!= std :: string :: npos)

* * * {

* * * * * formattedText.insert(index," \ r");

* * * * * index + = 2;

* * *}

* *} while(index< formattedText.size());


* * //在这里调试formattedText的内容


* *返回0;


}


但是,它取决于std :: string :: npos总是小于字符串的大小

字符串,我不确定这是否安全。



没关系/安全。一个std :: string不能有一个length()(因为它比std :: string :: npos更好地是
std :: string,size())。所以,你的代码

应该是完全没问题的,当所有

的''\ n''实例都被处理时,它总会退出.-隐藏引用的文字 -


- 显示引用的文字 -



理想情况下,你应该处理length_error异常。


Is std::string::npos always going to be less than any std::string ''s size()?

I am trying to handle a replacement of all occurances of a substr, in which
the replacement also contains the substr. Yick. All I could come up with is:

#include <string>

int main()
{
std::string text;
text = "\nThis is a test line with newlines\n<-here and \n<-here and
\n\n<-two here";

// Format the text
std::string formattedText(text);

// Change every occurrance of "\n" to "\r\n"
std::string::size_type index = 0;

do
{
index = formattedText.find(''\n'', index);
if( index != std::string::npos )
{
formattedText.insert(index, "\r");
index += 2;
}
} while( index < formattedText.size() );

// debugging contents of formattedText here

return 0;
}

However, it depends on std::string::npos always being less than the size of
the string and I am not certain whether that is safe or not.

解决方案

On Jan 10, 4:52 pm, "Christopher Pisz" <some...@somewhere.netwrote:

Is std::string::npos always going to be less than any std::string ''s size()?

I am trying to handle a replacement of all occurances of a substr, in which
the replacement also contains the substr. Yick. All I could come up with is:

#include <string>

int main()
{
std::string text;
text = "\nThis is a test line with newlines\n<-here and \n<-here and
\n\n<-two here";

// Format the text
std::string formattedText(text);

// Change every occurrance of "\n" to "\r\n"
std::string::size_type index = 0;

do
{
index = formattedText.find(''\n'', index);
if( index != std::string::npos )
{
formattedText.insert(index, "\r");
index += 2;
}
} while( index < formattedText.size() );

// debugging contents of formattedText here

return 0;

}

However, it depends on std::string::npos always being less than the size of
the string and I am not certain whether that is safe or not.

I know this is a horrible solution, but what about something along the
lines copying the contents of the string to another string?
int i;

for (i = 0; i < string1.size(); i++)
{
if (string1[i] == ''\n'') string2 += "\r\n";
else string2 += string1[i];
}
It''s slow and bloated it does the job in a straightforward way.


On Jan 10, 8:52*am, "Christopher Pisz" <some...@somewhere.netwrote:

Is std::string::npos always going to be less than any std::string ''s size()?

I am trying to handle a replacement of all occurances of a substr, in which
the replacement also contains the substr. Yick. All I could come up with is:

#include <string>

int main()
{
* *std::string text;
* *text = "\nThis is a test line with newlines\n<-here and \n<-here and
\n\n<-two here";

* *// Format the text
* *std::string formattedText(text);

* *// Change every occurrance of "\n" to "\r\n"
* *std::string::size_type index = 0;

* *do
* *{
* * * index = formattedText.find(''\n'', index);
* * * if( index != std::string::npos )
* * * {
* * * * *formattedText.insert(index, "\r");
* * * * *index += 2;
* * * }
* *} while( index < formattedText.size() );

* *// debugging contents of formattedText here

* *return 0;

}

However, it depends on std::string::npos always being less than the size of
the string and I am not certain whether that is safe or not.

It is ok/safe. An std::string cannot have a length() (since it is
std::string, size()) greater than std::string::npos. So, your code
should be perfectly fine, it will always exit the do-while when all
instances of ''\n'' have been dealt with.


On Jan 10, 1:29*pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

On Jan 10, 8:52*am, "Christopher Pisz" <some...@somewhere.netwrote:


Is std::string::npos always going to be less than any std::string ''s size()?

I am trying to handle a replacement of all occurances of a substr, in which
the replacement also contains the substr. Yick. All I could come up withis:

#include <string>

int main()
{
* *std::string text;
* *text = "\nThis is a test line with newlines\n<-here and \n<-here and
\n\n<-two here";

* *// Format the text
* *std::string formattedText(text);

* *// Change every occurrance of "\n" to "\r\n"
* *std::string::size_type index = 0;

* *do
* *{
* * * index = formattedText.find(''\n'', index);
* * * if( index != std::string::npos )
* * * {
* * * * *formattedText.insert(index, "\r");
* * * * *index += 2;
* * * }
* *} while( index < formattedText.size() );

* *// debugging contents of formattedText here

* *return 0;

}

However, it depends on std::string::npos always being less than the sizeof
the string and I am not certain whether that is safe or not.


It is ok/safe. An std::string cannot have a length() (since it is
std::string, size()) greater than std::string::npos. So, your code
should be perfectly fine, it will always exit the do-while when all
instances of ''\n'' have been dealt with.- Hide quoted text -

- Show quoted text -

Ideally, you should handle the length_error exception.


这篇关于std :: string :: npos always&lt; std :: string :: size()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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