Using vector two-dimensional array to write a phone book
The code is as follows:
#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; Storage name vector<string> num; Store a person's phone number vector<vector<string>> phone_num; Store owner's phone number input >> temp;storage.push_back (temp), while (input >> temp) {if (Isalpha (Temp[0])) { Storage.push_back (temp);p hone_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;}
The following are the contents of Temp.txt
Morgan 2015552368 8625550123
Drew 9735550130
Lee 6095550132 20155550175 805550000
Here are the results of the program run
Using vector two-dimensional arrays