在map中保存struct,及map的文件输入输出

自己写的代码,因为我记忆力差的惊人,所以一方面以后我用到这个代码直接复制粘贴反正会忘,就不费力记了。另一方面,看到国内很多这样现成的例子少,所以方便大家。

用函数的方法实现:

从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;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值