将字符串拆分为数组字 [英] Splitting a string into an array words

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

问题描述

嗯,这个标题非常具有描述性;我怎样才能获得这样的

输入线:


getline(cin,mostrecentline);


并分成一个(灵活的)字符串数组。例如:执行此操作

操作

将转到:


项目0:执行

项目1:这个

项目2:行动


提前致谢,

Simon

Well, the title''s pretty descriptive; how would I be able to take a
line of input like this:

getline(cin,mostrecentline);

And split into an (flexible) array of strings. For example: "do this
action"
would go to:

item 0: do
item 1: this
item 2: action

Thanks in advance,
Simon

推荐答案

文章< 11 ********************** @ m79g2000cwm.googlegroups .com>,

" Simon" < Si *********** @ gmail.comwrote:
In article <11**********************@m79g2000cwm.googlegroups .com>,
"Simon" <Si***********@gmail.comwrote:

好​​吧,这个标题很有描述性;我怎样才能获得这样的

输入线:


getline(cin,mostrecentline);


并分成一个(灵活的)字符串数组。例如:执行此操作

操作

将转到:


项目0:执行

项目1:此

项目2:行动
Well, the title''s pretty descriptive; how would I be able to take a
line of input like this:

getline(cin,mostrecentline);

And split into an (flexible) array of strings. For example: "do this
action"
would go to:

item 0: do
item 1: this
item 2: action



#include< vector>

# include< string>

#include< iostream>

#include< iterator>

//其他包括必要


模板< typename OutIt>

void split(const std :: string& in,OutIt result)

{

//在这里添加代码.. 。

}


int main(){

string seed =" step1";

vector< stringresult;

split(seed,back_inserter(result));

assert(result.size()== 1);

assert(result [0] ==" step1");

std :: cout<< 你做到了!干得好!\ n"

}


运行上述程序。有机会标记为在此处添加代码部分

,直到程序编译并打印出你做到了!干得好!"。


如果确实如此,请回复此处的代码,我将帮助您完成

下一步。

#include <vector>
#include <string>
#include <iostream>
#include <iterator>
// other includes as necessary

template < typename OutIt >
void split( const std::string& in, OutIt result )
{
// add code here...
}

int main() {
string seed = "step1";
vector<stringresult;
split( seed, back_inserter( result ) );
assert( result.size() == 1 );
assert( result[0] == "step1" );
std::cout << "You did it! Good job!\n"
}

Run the above program. Make chances to the part labeled "add code here"
until the program compiles and prints out "You did it! Good job!".

When it does, post back here with the code and I''ll help you with the
next step.


Simon写道:
Simon wrote:

嗯,这个标题很有描述性;我怎样才能获得这样的

输入线:


getline(cin,mostrecentline);


并分成一个(灵活的)字符串数组。
Well, the title''s pretty descriptive; how would I be able to take a
line of input like this:

getline(cin,mostrecentline);

And split into an (flexible) array of strings.



灵活是什么意思,你想使用哪些分隔符?

What do you mean by "flexible", and which separators do you want to use?


例如:做这个

动作

将转到:


项目0:做

item 1:this

item 2:action
For example: "do this
action"
would go to:

item 0: do
item 1: this
item 2: action



在这种情况下,我会使用字符串流和运算符> > ;.

In this case, I''d use a stringstream and operator>>.


Simon< Si *********** @ gmail.comwrote:
Simon <Si***********@gmail.comwrote:

嗯,这个标题很有描述性;我怎样才能获得这样的

输入线:


getline(cin,mostrecentline);


并分成一个(灵活的)字符串数组。例如:执行此操作

操作

将转到:


项目0:执行

item 1:this

item 2:action
Well, the title''s pretty descriptive; how would I be able to take a
line of input like this:

getline(cin,mostrecentline);

And split into an (flexible) array of strings. For example: "do this
action"
would go to:

item 0: do
item 1: this
item 2: action



如果要按空格分割单词,可以创建一个

std :: istringstream并将它们推入std :: vector< std :: string> ;.


类似于:(未经测试和未编译)


std :: istringstream line(mostrecentline);

std :: vector< std :: stringwords;

std :: string temp;


while(line> temp){

words.push_back(temp);

}

您需要#include< sstream>,< string>和< vector for this

方法。


-

Marcus Kwok

用''net''替换''invalid''回复

If you are splitting the words by whitespace, you could create a
std::istringstream and push them into a std::vector<std::string>.

Something like: (untested and uncompiled)

std::istringstream line(mostrecentline);
std::vector<std::stringwords;
std::string temp;

while (line >temp) {
words.push_back(temp);
}

You will need to #include <sstream>, <string>, and <vectorfor this
method.

--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply


这篇关于将字符串拆分为数组字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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