自己写的代码,因为我记忆力差的惊人,所以一方面以后我用到这个代码直接复制粘贴反正会忘,就不费力记了。另一方面,看到国内很多这样现成的例子少,所以方便大家。
用函数的方法实现:
从map输出到文件,map的value是struct
#include<map>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct studentID {
string name;
string address;
};
studentID MakeStudentID(const string name, const string address)
{
studentID c={name, address};
return c;
}
int main()
{
int number[2]={49,21};
map<int, studentID> studentID_table;
studentID_table.insert(make_pair(number[0],MakeStudentID("Tom","Dalian")));
studentID_table.insert(make_pair(number[1],MakeStudentID("Lee","Shenyangi")));
ofstream outFile("newfile.txt");
map<int, studentID>::const_iterator map_it = studentID_table.begin();
while(map_it!= studentID_table.end())
{
outFile << map_it->first<<endl;
outFile << map_it->second.name<<endl;
outFile << map_it->second.address<<endl;
map_it++;
}
return 0;
outFile.close();
}
用构造constructor的方法:
#include<map>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct studentID {
studentID(): name(),address() {}
studentID(string newname, string newaddress): name(newname), address(newaddress) {}
string name;
string address;
};
int main()
{
int number[2]={49,21};
map<int, studentID> studentID_table;
studentID_table.insert(make_pair(number[0],studentID("Tom","Dalian")));
studentID_table.insert(make_pair(number[1],studentID("Lee","Shenyangi")));
ofstream outFile("newfile.txt");
map<int, studentID>::const_iterator map_it = studentID_table.begin();
while(map_it!= studentID_table.end())
{
outFile << map_it->first<<endl;
outFile << map_it->second.name<<endl;
outFile << map_it->second.address<<endl;
map_it++;
}
return 0;
outFile.close();
}
其中关于向map中插入值一段:
int number[2]={49,21};
map<int, studentID> studentID_table;
studentID_table.insert(make_pair(number[0],studentID("Tom","Dalian")));
studentID_table.insert(make_pair(number[1],studentID("Lee","Shenyangi")));
等价于:
map<int, studentID> studentID_table;
studentID_table[49]=studentID("Tom","Dalian");
studentID_table[21]=studentID("Lee","Shenyangi");
输出结果如下
-bash-4.1$ more newfile.txt
21
Lee
Shenyangi
49
Tom
Dalian
不管创建map时哪个学号先创建,顺序输出时永远是按照key值由小到大排列的,这是map数据结构本身的优点
从文件输入:
#include<map>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct studentID {
studentID(): name(),address() {}
studentID(string newname, string newaddress): name(newname), address(newaddress) {}
string name;
string address;
};
int main()
{
map<int, studentID> studentID_table;
int key;
string temp_name, temp_address;
ifstream inFile("newfile.txt");
while(!inFile.eof())
{
inFile >> key >> temp_name >> temp_address;
studentID_table[key]= studentID(temp_name,temp_address);
}
inFile.close();
map<int, studentID>::iterator map_it = studentID_table.begin();
for( map_it = studentID_table.begin(); map_it != studentID_table.end(); map_it++) {
cout << "number:" <<endl;
cout << map_it->first << endl;
cout << "the name is" << endl;
cout << map_it->second.name<<endl;
cout << "the address is"<< endl;
cout << map_it->second.address<<endl;
}
return 0;
}
查找key是否存在在map中,如果存在就输出,如果不存在什么也不做
#include<map>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct studentID {
studentID(): name(),address() {}
studentID(string newname, string newaddress): name(newname), address(newaddress) {}
string name;
string address;
};
int main()
{
map<int, studentID> studentID_table;
int key;
string temp_name, temp_address;
ifstream inFile("newfile.txt");
while(!inFile.eof())
{
inFile >> key >> temp_name >> temp_address;
studentID_table[key]= studentID(temp_name,temp_address);
}
inFile.close();
map <int, studentID>::iterator map_it;
if((map_it=studentID_table.find(66))!= studentID_table.end()) {
cout << studentID_table.find(66)->second.name << studentID_table.find(66)->second.address << endl;
}
return 0;
}