通过空格将字符串解析为向量
Parse a string by whitespace into a vector
假设我有一串数字
"1 2 3 4 5 6"
我想拆分这个字符串并将每个数字放入我的向量中的不同插槽中。最好的方法是什么
使用 istringstream 将字符串称为流,并使用运算符>>
获取数字。如果字符串包含换行符和制表符,它也将起作用。下面是一个示例:
#include <vector>
#include <sstream> // for istringstream
#include <iostream> // for cout
using namespace std; // I like using vector instead of std::vector
int main()
{
char *s = "1 2 3 4 5";
istringstream s2(s);
vector<int> v;
int tmp;
while (s2 >> tmp) {
v.push_back(tmp);
}
// print the vector
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << endl;
}
}
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
std::vector<std::string> StringToVector(std::string const& str, char const delimiter);
int main(){
std::string str{"1 2 3 4 5 6 "};
std::vector<std::string> vec{StringToVector(str, ' ')};
//print the vector
for(std::string const& item : vec){
std::cout << "[" << item << "]";
}
return EXIT_SUCCESS;
}
std::vector<std::string> StringToVector(std::string const& str, char const delimiter){
std::vector<std::string> vec;
std::string element;
//we are going to loop through each character of the string slowly building an element string.
//whenever we hit a delimiter, we will push the element into the vector, and clear it to get ready for the next element
for_each(begin(str),end(str),[&](char const ch){
if(ch!=delimiter){
element+=ch;
}
else{
if (element.length()>0){
vec.push_back(element);
element.clear();
}
}
});
//push in the last element if the string does not end with the delimiter
if (element.length()>0){
vec.push_back(element);
}
return vec;
}
g++ -std=c++0x -o main main.cpp
这样做的好处是永远不会将空字符串推入向量。
您还可以选择所需的分隔符。
也许你可以写一些其他的:一个用于字符向量,或者分隔符可以是字符串?
:)祝你好运!
#include <vector>
#include <string>
#include <sstream>
int str_to_int(const string& str){
stringstream io;
int out;
io<<str;
io>>out;
return out;
};
vector<int> Tokenize(string str, string delimiters = " ")
{
vector<int> tokens;
string::size_type nwpos; //position of first non white space, which means it is first real char
nwpos = str.find_first_not_of(delimiters, 0); //ignore the whitespace before the first word
string::size_type pos = str.find_first_of(delimiters, nwpos);
while (string::npos != pos || string::npos != nwpos)
{
// Found a token, add it to the vector.
tokens.push_back(str_to_int(str.substr(nwpos, pos - nwpos)));
// Skip delimiters. Note the "not_of"
nwpos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, nwpos);
}
return tokens;
};
尝试:
#include <sstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
int main()
{
// The data
std::string data = "1 2 3 4 5 6";
// data in a stream (this could be a file)
std::stringstream datastream(data);
// Copy the data from the stream into a vector.
std::vector<int> vec;
std::copy(std::istream_iterator<int>(datastream), std::istream_iterator<int>(),
std::back_inserter(vec)
);
// We can also copy the vector to the output (or any other stream).
std::copy(vec.begin(), vec.end(),
std::ostream_iterator<int>(std::cout, "n")
);
}
相关文章:
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 构造<int>具有 2 个字符串文字的向量
- 如何将元素添加到向量的字符串位置
- 将向量解析<string>为字符串
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 在 C++11 中,如何查找并返回以给定字符串开头的字符串向量中的所有项?
- 如何使用向量字符串令牌构建表达树
- 将 argv 复制到新的全局向量字符串
- 如何读取控制台输入并将其存储到向量字符串中
- 向量字符串push_back在C 中不起作用
- C char数组和向量字符串
- 可以直接初始化向量<字符串 *> 而不使用 new 吗?
- 迭代向量字符串的字符(C++禁止指针和整数之间的比较)
- 比较两个向量字符串的相似性C++
- 我有麻烦将字符串放入向量字符串
- 如何从一个向量字符串得到一个字符串的联合集
- 函数返回向量/字符串,但不返回数组
- 向量向量字符串固定大小
- c++向量字符串排序不工作
- C++将字符串数组[]复制到向量<字符串>