标签:blog http io ar os 使用 sp for on
用vector二维数组写一个电话薄
代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cctype>
int main(int argc, char *argv[])
{
using namespace std;
string file_name = "F:\\Code\\C++\\C++ Primer 5th\\C++ Primer 5th\\temp\\temp.txt";
ifstream input(file_name);
if (!input) {
cerr << "couldn‘t open: " << file_name << endl;
}
string temp;
int count = 0;
vector<string> storage; // 存储姓名
vector<string> num; // 存储某一个人的电话号码
vector<vector<string>> phone_num; // 存储所有人的电话号码
input >> temp;
storage.push_back(temp);
while (input >> temp) {
if (isalpha(temp[0])) {
storage.push_back(temp);
phone_num.push_back(num);
num.clear();
}
else {
num.push_back(temp);
}
}
phone_num.push_back(num);
for (auto name : storage) {
cout << name << " ";
}
cout << endl;
for (auto &row : phone_num) {
for (auto &col : row) {
cout << col << " ";
}
cout << endl;
}
input.close();
return 0;
}
以下是temp.txt的内容
morgan 2015552368 8625550123
drew 9735550130
lee 6095550132 20155550175 805550000
以下是程序运行结果

标签:blog http io ar os 使用 sp for on
原文地址:http://www.cnblogs.com/hcxc/p/4116237.html