向量问题,也许有点手
Vector problems, a little hand maybe?
我正在尝试创建一个程序,该程序读取文本文件中的内容,然后在向量中的两个元素之间交换位置。
文本文件具有以下顺序:名称,姓氏,注册,_number和city_adress。
清单约100人。我想知道是否有一种方法不包括整行并将其结束在空间中。
这是我的代码的样子!
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> line;
string information;
ifstream myfile ("Namn.txt");
if (myfile.is_open()) {
while (getline(myfile, information)) {
line.push_back (information);
}
myfile.close();
}else cout << "Filen gick inte att öppna!";
for (unsigned int i = 0; i < line.size(); i++) {
cout << line[i] << endl;
}
return 0;
}
您可以使用fstream
读取文件并同时解析您的文件,然后将信息的所有元素放在向量的向量中,然后您可以混合并匹配您喜欢的信息位,这样:
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
int main()
{
std::string name;
std::string surname;
std::string registration;
std::string location;
std::vector<std::vector<std::string>> information;
std::ifstream myfile("Namn.txt");
while (myfile >> name >> surname >> registration >> location)
information.push_back({name, surname, registration, location});
// print
for (auto const &info : information)
{
std::cout << info[1] << " " << info[0] << " " << info[2] << " " << info[3] << std::endl;
}
return 0;
}
打印:
Andersson Rune 12873645 Stockholm
Isaksson Peter 12873645 Uppsala
Gustavsson Fredrik 12873645 Gothenborg
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 向量问题,也许有点手