如何在c++中去除向量中的空格
how to remove spaces in vector in c++
下面是我为其编写的用于拆分字符串并存储在向量中的程序。如何从字符串中检索特定字段,并使用pipeline(|)符号再次连接期望的字符串。
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
vector<string> split(string str, char delimiter)
{
vector<string> internal;
stringstream ss(str);
string tok;
while(getline(ss, tok, delimiter))
{
internal.push_back(tok);
}
return internal;
}
int main(int argc, char **argv)
{
string myCSV = "Event#:11918124|1234|67893|USD||||444400090|||||302|45|USA|||||";
vector<string> sep = split(myCSV, '|');
for(int i = 0; i < sep.size(); ++i);
cout << sep[0] << "|" << sep[3] << "|" << sep[7] << "|" << sep[14] << endl;
}
输出:
Event#:11918124
1234
67893
USD
444400090
302
45
USA
预期输出:
Event#:11918124|USD|444400090|USA
有人能帮我学习c++吗?我是c++的新手
您所需要做的就是连接记录中所需的字段。它们总是出现在相同的索引(0、3、7、14)处。所以在你的情况下,这很容易:
cout << sep[0] << "|" << sep[3] << "|" << sep[7] << "|" << sep[14] << endl;
顺便说一句,你对代码的"output:"是错误的。
相关文章:
- 2D数组来自文本输入,中间有空格
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 如何接受字符串中包含空格的字符串向量
- 如何使字符串向量与用户输入保持空格
- 如何在c++中去除向量中的空格
- C++ 从空格分隔的文本文件中填充字符串向量
- 当数据包含空格时,如何将制表符分隔文件的内容加载到C++字符串的 2D 向量中
- 通过空格将字符串解析为向量
- 读取由空格分隔的字符串到C++向量中
- 将由空格分隔的浮点数组成的文本文件映射到浮点数向量的内存