?? main.cpp
字號:
#include <map>
#include <iostream>
struct StudentRecord{ //學生記錄結構體
struct StudentInfo{
char* name;
int year;
char* addr;
};
StudentRecord(int id_, char* name_, int year_, char* addr_){
id=id_;
sf.name=name_;
sf.year=year_;
sf.addr=addr_;
}
int id; //學號,作鍵值
StudentInfo sf; //其他信息
};
int main(void){
using namespace std;
//創建map容器對象m
typedef map<int, StudentRecord::StudentInfo> studentmap;
studentmap m;
pair<studentmap::iterator, bool > p;
//插入第一條學生記錄
StudentRecord student1=StudentRecord(1, "焦焦", 21, "北京");
pair<int, StudentRecord::StudentInfo> pairStudent1(student1.id, student1.sf);
p=m.insert(pairStudent1);
if(!p.second)
cout << "插入學生記錄失敗:\n"
<< student1.id << ' '
<< student1.sf.name << ' '
<< student1.sf.year << ' '
<< student1.sf.addr << ' '
<< endl << endl;
//插入第二條學生記錄
StudentRecord student2=StudentRecord(2, "敦介", 18, "上海");
pair<int, StudentRecord::StudentInfo> pairStudent2(student2.id, student2.sf);
p=m.insert(pairStudent2);
if(!p.second)
cout << "插入學生記錄失敗:\n"
<< student2.id << ' '
<< student2.sf.name << ' '
<< student2.sf.year << ' '
<< student2.sf.addr << ' '
<< endl << endl;
//插入第三條學生記錄
StudentRecord student3=StudentRecord(3, "譯尹", 20, "深圳");
pair<int, StudentRecord::StudentInfo> pairStudent3(student3.id, student3.sf);
p=m.insert(pairStudent3);
if(!p.second)
cout << "插入學生記錄失敗:\n"
<< student3.id << ' '
<< student3.sf.name << ' '
<< student3.sf.year << ' '
<< student3.sf.addr << ' '
<< endl << endl;
//插入鍵值重復的學生記錄,失敗
StudentRecord student4=StudentRecord(3, "李強", 26, "天津");
pair<int, StudentRecord::StudentInfo> pairStudent4(student4.id, student4.sf);
p=m.insert(pairStudent4);
if(!p.second)
cout << "插入學生記錄失敗:\n"
<< student4.id << ' '
<< student4.sf.name << ' '
<< student4.sf.year << ' '
<< student4.sf.addr << ' '
<< endl << endl;
//記錄搜索
studentmap::iterator i=m.find(2);
cout << "搜索出學號為2的記錄:\n"
<< (*i).first << ' '
<< (*i).second.name << ' '
<< (*i).second.year << ' '
<< (*i).second.addr << ' '
<< endl << endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -