int main()
{
int arr[] = { 44, 45, 64, 6, 2, 1, 465, 4 };
int len = sizeof(arr) / sizeof(arr[0]);
map<int, int> mp1;
//map<int, int> mp2(arr,arr+len);//err 无法通过普通迭代器区间构造map对象
//插入
/*
1.通过数据类型 对值类型进行重定义
//cout << typeid(map<int, int>::value_type).name() << endl;
struct std::pair<int const ,int>//pair结构体将键类型和值类型打包在一起
*/
typedef struct std::pair<int const, int> PAIR;
PAIR pair1(1, 111);
mp1.insert(pair1);
/*
2. 对值类型进行重定义
*/
typedef map<int, int>::value_type VALUETYPE;
VALUETYPE val1(2, 222);
mp1.insert(val1);
/*
3.
*/
mp1[3] = 333;
map<int, int>::iterator it = mp1.begin();
for (; it != mp1.end(); ++it)
{
cout << it->first << " ";
cout << it->second << " ";
}
}
class Friend
{
public:
Friend(string name = "", int id = 0)
:mname(name), mid(id)
{}
bool operator<(const Friend& rhs)const
{
return mid < rhs.mid;
}
int getId()
{
return mid;
}
private:
friend ostream& operator<<(ostream& out, const Friend& rhs);
string mname;
int mid;
};
ostream& operator<<(ostream& out, const Friend& rhs)
{
out << rhs.mname << " ";
out << rhs.mid << " ";
return out;
}
class FriendList//朋友圈类
{
public:
void addFriend(Friend& f)
{
myset.insert(f);
}
void delFriend(Friend& f)
{
myset.erase(f);
}
void showFriend()//查看朋友 相当于遍历整个容器
{
copy(myset.begin(), myset.end(), ostream_iterator<Friend>(cout, "...."));
}
private:
set<Friend> myset;
//set<Friend,greater<Friend>()> myset;//降序排序,要提供>比较
};
int main()
{
//按照这种方式给map插入元素时,值类型如果是自定义类型,一定要有默认的构造函数
Friend f1("zzg", 1);
map<int, Friend> mp ;
mp[f1.getId()] = f1;
return 0;
}