如何分隔字符串并将每个部分存储在不同的向量中
How to I delimit a string and store each part in different vectors
我有一个有很多行的文本文件,一个例子是
约翰:学生:商业
五月:讲师:数学
鲍勃:学生:数学
如何将它们拆分并存储在不同的向量中,例如:
矢量 1:约翰、梅、鲍勃
向量2:学生、讲师、学生
矢量3:商业、数学、数学
我当前的代码是:
ifstream readDetails(argv[1]);
while (getline (readEvents, line, ':')){
cout << line << endl;
}
它只拆分字符串,但我想不出任何方法来分离字符串并将它们存储到向量中。
您可以创建向量的向量并使用令牌索引。
如果流有时可以具有较少的令牌,则需要处理这种情况。
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main() {
const int NUM_TOKENS = 3;
std::vector<std::vector<std::string>> v(NUM_TOKENS);
int token = 0;
std::string str("Mary:Had:Lamb");
std::istringstream split(str);
std::string line;
while (std::getline (split, line, ':')){
v[token++].push_back(line);
if ( token == NUM_TOKENS )
token = 0;
}
return 0;
}
这可以使用嵌套向量(又名 2D 向量(和嵌套循环来完成。外循环用于将输入拆分为行,内循环用于拆分每行的标记,这些标记由':'
分隔。
#include <string>
#include <sstream>
#include <vector>
int main()
{
std::istringstream in{
"john:student:businessn"
"may:lecturer:mathn"
"bob:student:math"
};
std::vector< std::vector< std::string > > v{ 3 }; // Number of tokens per line
std::string line;
// Loop over the lines
while( std::getline( in, line ) ) {
std::istringstream lineStrm{ line };
// Loop to split the tokens of the current line
for( auto& col : v ) {
std::string token;
std::getline( lineStrm, token, ':' );
col.push_back( token );
}
}
}
科利鲁的现场演示。
创建字符串向量并相应地存储数据。不使用std::istringstream
,你基本上可以利用 String 类附带的substr()
和find()
,因此:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main ()
{
ifstream inFile("data.txt");
string line;
vector<string> vStr1,vStr2, vStr3;
while( std::getline( inFile, line ) ){
string::size_type idx1 = line.find(":");
string::size_type idx2 = line.rfind(":");
vStr1.push_back(line.substr(0,idx1));
vStr2.push_back(line.substr(idx1+1,idx2-idx1-1));
vStr3.push_back(line.substr(idx2+1));
}
cout << "vector1: ";
for(int i(0); i < vStr1.size(); ++i ){
cout << vStr1[i] << " ";
}
cout << endl;
cout << "vector2: ";
for(int i(0); i < vStr2.size(); ++i ){
cout << vStr2[i] << " ";
}
cout << endl;
cout << "vector3: ";
for(int i(0); i < vStr3.size(); ++i ){
cout << vStr3[i] << " ";
}
cout << endl;
return 0;
}
结果是
vector1: john may bob
vector2: student lecturer student
vector3: business math math
相关文章:
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 在c++中获取两个大int,并将它们存储在数组中
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 为什么 bool 和 _Bool 如果它们在内存中占用 1 个字节,它们只能存储 0 或 1
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- Boost program_options将多个配置文件解析结果存储到一个parsed_options中
- 如何存储包含多个空格的字符串 c++
- 单个变量如何存储多个值?
- 如何在 c++ 中存储具有值的四个参数的元组
- 初始化一个由 p 指向的新 INTSTK,它最多可以存储 m 个
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- C++ 如何在包含两个变量的结构中存储与变量不同数量的值?
- 将一个阵列中的数据存储到另外两个阵列中不起作用
- 一个 2D 数组,并按行存储值.第一个循环用于行索引,第二个循环用于列索引
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支
- 将文本文件中的文本存储到六个不同的矢量中(C++)
- 如何从字符数组C++中提取2个整数并将它们存储在2个变量中(初学者)
- 是否可以使用 Wojciech Mula 算法将 __m256i 个 32 位字而不是 4 个 64 位字存储为弹出计数和结果?
- 是否可以将多个结构作为一个数据包存储在一个函数中,然后传递给其他函数并在那里提取?
- 如果静态变量只为程序的整个部分存储了一个副本,为什么我不能使用静态变量交换 2 个数字?