C++ Read matrices from file with multiple delimiters

This question already has an answer here: Parse (split) a string in C++ using string delimiter (standard C++) [duplicate] 11 answers The most elegant way to iterate the words of a string [closed] 74 answers string line; while(getline(infile, line, '|')) { stringstream rowstream(line); string row; while(getline(rowstream, row, ';')) { stringstream elementstream(row)

C ++从具有多个分隔符的文件中读取矩阵

这个问题在这里已经有了答案: 使用字符串分隔符(标准C ++)在C ++中解析(拆分)一个字符串11个答案 迭代字符串最优雅的方式[已关闭] 74个答案 string line; while(getline(infile, line, '|')) { stringstream rowstream(line); string row; while(getline(rowstream, row, ';')) { stringstream elementstream(row); string element; while(getline(elementstream,

creating a string split function in C++

Possible Duplicate: Splitting a string in C++ Im trying to create a function that mimics the behavior of the getline() function, with the option to use a delimiter to split the string into tokens. The function accepts 2 strings (the second is being passed by reference) and a char type for the delimiter. It loops through each character of the first string, copying it to the second string and

在C ++中创建一个字符串拆分函数

可能重复: 在C ++中分割一个字符串 我试图创建一个模仿getline()函数行为的函数,并使用分隔符将字符串拆分为标记。 该函数接受2个字符串(第二个被引用传递)和分隔符的char类型。 它遍历第一个字符串的每个字符,将其复制到第二个字符串,并在到达分隔符时停止循环。 如果第一个字符串在分隔符后面包含更多字符,则返回true否则返回false 。 最后一个字符的位置被保存在一个静态变量中。 由于某种原因程序正在进入一

parse string to vector of int

This question already has an answer here: The most elegant way to iterate the words of a string [closed] 74 answers You can use std::stringstream . You will need to #include <sstream> apart from other includes. #include <sstream> #include <vector> #include <string> std::string myString = "10 15 20 23"; std::stringstream iss( myString ); int number; std::vector<i

将字符串解析为int的向量

这个问题在这里已经有了答案: 迭代字符串最优雅的方式[已关闭] 74个答案 你可以使用std::stringstream 。 除了其他包含之外,您需要#include <sstream> 。 #include <sstream> #include <vector> #include <string> std::string myString = "10 15 20 23"; std::stringstream iss( myString ); int number; std::vector<int> myNumbers; while ( iss >> number ) myNumbers.push_

Splitting strings in C++

This question already has an answer here: The most elegant way to iterate the words of a string [closed] 74 answers this works nicely for me :), it puts the results in elems . delim can be any char . 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::ge

在C ++中分割字符串

这个问题在这里已经有了答案: 迭代字符串最优雅的方式[已关闭] 74个答案 这对我很好:),它把结果放在elems 。 delim可以是任何char 。 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

c++ Split string by a character?

This question already has an answer here: The most elegant way to iterate the words of a string [closed] 74 answers stringstream can do all these. Split a string and store into int array: string str = "102:330:3133:76531:451:000:12:44412"; std::replace(str.begin(), str.end(), ':', ' '); // replace ':' by ' ' vector<int> array; stringstream ss(str); int temp; while (ss >> temp

c ++字符串拆分字符串?

这个问题在这里已经有了答案: 迭代字符串最优雅的方式[已关闭] 74个答案 stringstream可以完成所有这些。 拆分一个字符串并存储到int数组中: string str = "102:330:3133:76531:451:000:12:44412"; std::replace(str.begin(), str.end(), ':', ' '); // replace ':' by ' ' vector<int> array; stringstream ss(str); int temp; while (ss >> temp) array.push_back(temp); // done! now array={102,3

Splitting a string by whitespace in c++

Possible Duplicates: C++: How to split a string? Splitting a string What is the best way to go about splitting a string up by whitespace in c++? I'd like to be able to split it based on tab, space, etc. and of course ignore multiple tabs/spaces/etc. in a row as well as not have issues with having those things at the end. Ultimately, I am going to end up storing this in a vector, bu

用c ++中的空格分割一个字符串

可能重复: C ++:如何分割一个字符串? 分割一个字符串 在c ++中用空格分隔字符串的最佳方法是什么? 我希望能够根据标签,空间等分割它,当然也可以忽略多个标签/空格/等。 并且最终没有这些事情的问题。 最终,我将最终将其存储在一个向量中,但是如果有一些简单的内置标准库分割方式,我可以轻松地在数据类型之间进行转换。 我正在使用g ++在UNIX机器上构建它, 而不是使用Microsoft Visual C ++ 有人可能会

Split a string into an array in C++

Possible Duplicate: How to split a string in 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: string line; ifst

用C ++将一个字符串拆分成一个数组

可能重复: 如何在C ++中分割字符串? 我有一个数据输入文件,每一行都是一个条目。 在每一行中,每个“字段”由一个空格“”隔开,所以我需要按空格分隔行。 其他语言有一个称为分裂(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

Split string by single spaces

Possible Duplicate: How to split a string in C++? I need to split a string by single spaces and store it into an array of strings. I can achieve this using a istringstream, but what I am not being able to achieve is this: I want every space to terminate the current word. So, if there are two spaces consecutively, one element of my array should be blank. For example: (underscore denote

按单个空格拆分字符串

可能重复: 如何在C ++中分割字符串? 我需要通过单个空格拆分字符串并将其存储到字符串数组中。 我可以使用istringstream实现这一点,但我无法实现的是这样的: 我希望每个空间都能结束当前的单词。 所以,如果连续有两个空格,我的数组中的一个元素应该是空白的。 例如: (下划线表示空间) This_is_a_string. gets split into: A[0] = This A[1] = is A[2] = a A[3] = string. This__is_a_string. gets split i

Splitting a C++ std::string using tokens, e.g. ";"

Best way to split a string in C++? The string can be assumed to be composed of words separated by the delimiter ; . From our guide lines point of view C string functions are not allowed and also Boost is also not allowed to use because of security concerns open source is not allowed. The best solution I have right now is: string str("denmark;sweden;india;us"); Above str should be stored i

使用标记分割C ++ std :: string,例如“;”

在C ++中分割字符串的最佳方法是什么? 该字符串可以被假定为由分隔符分隔的单词组成; 。 从我们的引导线的角度来看,C字符串函数是不允许的,并且由于安全考虑,不允许使用开源。 我现在最好的解决方案是: string str("denmark;sweden;india;us"); 上面的str应该作为字符串存储在向量中。 我们怎样才能做到这一点? 感谢您的投入。 我发现std :: getline()往往是最简单的。 可选的分隔符参数意味着它不仅用于

Parse (split) a string in C++ using string delimiter (standard C++)

This question already has an answer here: The most elegant way to iterate the words of a string [closed] 74 answers You can use the std::string::find() function to find the position of your string delimiter, then use std::string::substr() to get a token. Example: std::string s = "scott>=tiger"; std::string delimiter = ">="; std::string token = s.substr(0, s.find(delimiter)); // token

使用字符串分隔符(标准C ++)在C ++中解析(拆分)字符串

这个问题在这里已经有了答案: 迭代字符串最优雅的方式[已关闭] 74个答案 您可以使用std::string::find()函数来查找字符串分隔符的位置,然后使用std::string::substr()来获取令牌。 例: std::string s = "scott>=tiger"; std::string delimiter = ">="; std::string token = s.substr(0, s.find(delimiter)); // token is "scott" find(const string& str, size_t pos = 0)函数返回字符串中str的第一个出现