C++ 将字符串拆分为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8448176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split a string into an array in C++
提问by Ahoura Ghotbi
Possible Duplicate:
How to split a string in C++?
可能的重复:
如何在 C++ 中拆分字符串?
I have an input file of data and each line is an entry. in each line each "field" is seperated by a white space " " so I need to split the line by space. other languages have a function called split (C#, PHP etc) but I cant find one for C++. How can I achieve this? Here is my code that gets the lines:
我有一个数据输入文件,每一行都是一个条目。在每一行中,每个“字段”都由一个空格“”分隔,所以我需要按空格分割行。其他语言有一个名为 split(C#、PHP 等)的函数,但我找不到用于 C++ 的函数。我怎样才能做到这一点?这是我获取行的代码:
string line;
ifstream in(file);
while(getline(in, line)){
// Here I would like to split each line and put them into an array
}
回答by Nawaz
#include <sstream> //for std::istringstream
#include <iterator> //for std::istream_iterator
#include <vector> //for std::vector
while(std::getline(in, line))
{
std::istringstream ss(line);
std::istream_iterator<std::string> begin(ss), end;
//putting all the tokens in the vector
std::vector<std::string> arrayTokens(begin, end);
//arrayTokens is containing all the tokens - use it!
}
By the way, use qualified-names such as std::getline
, std::ifstream
like I did. It seems you've written using namespace std
somewhere in your code which is considered a bad practice. So don't do that:
顺便说一句,像我一样使用限定名称std::getline
,std::ifstream
例如 。您似乎using namespace std
在代码中的某处编写了被认为是不好的做法。所以不要这样做:
回答by Benjamin Lindley
vector<string> v;
boost::split(v, line, ::isspace);
http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768
http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768
回答by Vijay
I have written a function for a similar requirement of mine, maybe you can use it!
我已经为我的类似需求编写了一个函数,也许你可以使用它!
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s+' ');
std::string item;
while(std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
回答by Software_Designer
The code below uses strtok()
to split a string into tokens and stores the tokens in a vector.
下面的代码用于strtok()
将字符串拆分为标记并将标记存储在向量中。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
char one_line_string[] = "hello hi how are you nice weather we are having ok then bye";
char seps[] = " ,\t\n";
char *token;
int main()
{
vector<string> vec_String_Lines;
token = strtok( one_line_string, seps );
cout << "Extracting and storing data in a vector..\n\n\n";
while( token != NULL )
{
vec_String_Lines.push_back(token);
token = strtok( NULL, seps );
}
cout << "Displaying end result in vector line storage..\n\n";
for ( int i = 0; i < vec_String_Lines.size(); ++i)
cout << vec_String_Lines[i] << "\n";
cout << "\n\n\n";
return 0;
}
回答by Tristram Gr?bener
C++ is best used with the almost-standard-library boost.
C++ 最好与几乎标准的库提升一起使用。
And an example : http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768
和一个例子:http: //www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768
回答by jli
Either use a stringstream
or read token by token from your ifstream
.
无论是使用stringstream
还是从通过令牌读取令牌ifstream
。
To do it with a stringstream:
要使用字符串流来做到这一点:
string line, token;
ifstream in(file);
while(getline(in, line))
{
stringstream s(line);
while (s >> token)
{
// save token to your array by value
}
}