需要的头文件test.h
#ifndef TEST_T
#define TEST_T
#include<iostream>
class Test{
private:
int test1;
int test2;
public:
void show();
int getTest1();
int getTest2();
void setTest1(int test1);
void setTest2(int test2);
Test(int test1,int test2);
Test(){}
};
void Test::setTest1(int test1){
this->test1=test1;
}
void Test::setTest2(int test2){
this->test2=test2;
}
Test::Test(int test1,int test2){
this->test1=test1;
this->test2=test2;
}
void Test::show(){
std::cout<<this->test1<<" "<<this->test2<<std::endl;
}
int Test::getTest1(){
return this->test1;
}
int Test::getTest2(){
return this->test2;
}
#endif // !TEST_T
主要内容,基本上实现了大部分的map的方法
#include<map>
#include<string>
#include"test.h"
int main(){
std::map<int,std::string> m;
m.insert(std::pair<int, std::basic_string<char>>(1,"a"));
m.insert(std::pair<int, std::basic_string<char>>(2,"b"));
//insert的第一种用法,std::pair<int,std::basic_string<char>>
//注意pair需要命名空间std
m.insert(std::map<int,std::string>::value_type(3,"c"));
//insert第二种用法,std::map<int,std::string>::value_type(3,"c")
m[4]="d";//类似于上两种,也是一种插入元素的方法
std::cout<<"equa_range(2).first->first:"<<m.equal_range(2).first->first
<<" equal_range(2).first->second:"<<m.equal_range(2).first->second<<"\n";
std::cout<<"equal_range(2).second->first:"<<m.equal_range(2).second->first<<
" equal_range(2).second->second:"<<m.equal_range(2).second->second<<"\n";
//equal_range(key值)返回两个iterator迭代器,first指向有key值实例,second指向下一个
for(auto &iter:m){
//foreach 遍历,iter.first指向map第一个值,iter.second指向map的第二个值
std::cout<<iter.first<<":"<<iter.second<<"|";
}
std::cout<<std::endl;
std::cout<<"使用cbegin()和cend()遍历"<<std::endl;
for(auto it=m.cbegin();it!=m.cend();it++){
// 类似于begin()和end(),但只返回值为const_iterator类型
// 注:点进去还是调用发现是返回begin()艹
// 注:貌似说是之后c++11版本后才能用
// 评价不如begin();
std::cout<<it->first<<":"<<it->second<<"|";
}
std::cout<<"\n使用crbegin()和crend()逆序遍历";
for(auto iter=m.crbegin();iter!=m.crend();iter++){
// 类似于rbegin()和rend(),但只返回const_reverse_iterator类型
// 注:同c++11后的产物
// 注:一样的返回的是rbegin()和rend()
// 注:rbegin()调用的end(),rend()调用的是begin(),属实是把指针玩明白了
std::cout<<iter->first<<":"<<iter->second<<"|";
}
std::cout<<std::endl;
std::cout<<"查看1:"<<m.find(1)->second<<" 删除1:"<<m.erase(1)<<std::endl;
//find()查找元素
//注:find()返回的是一个iterator类型的值
//erase(可以是key值也可以是迭代器)
std::cout<<"遍历查看map:"<<std::endl;
std::map<int,std::string>::iterator iter=m.begin();
//begin()返回指向map头部的迭代器
while (iter!=m.end()){
//end()返回指向map末尾的迭代器
std::cout<<iter->first<<":"<<iter->second<<"|";
iter++;
}
//迭代器遍历map,iter->first指向map第一个值,iter->second指向map的第二个值
std::cout<<std::endl;
std::cout<<"clear()之前的大小"<<m.size();
m.clear();
std::cout<<" clear()之后的大小"<<m.size()<<std::endl;
m.emplace(1,"a");
// C++11后才能用的新的插入方法
// 对于太复杂的数据插入还是用insert()方法好用些,效率比insert()高?不是很懂
m.emplace_hint(m.end(),0,"b");
// C++11后的新方法
// 需要一个const_iterator类型的构造器(begin()和end()都可以放回const_iterator类型,或许这就是cbegin()和cend()用处?)
// 同上
for(auto iter:m){
std::cout<<iter.first<<":"<<iter.second<<"|";
}
std::cout<<"\n";
std::cout<<m.empty()<<std::endl;
//--------------------------------------------------------
Test t1(1,2),t2(3,4);
std::map<int,Test> test;
// map中的class类一定要有个空构造器 类似 Test(){}
test.insert(std::map<int,Test>::value_type(1,t1));
test[2]=t2;
std::cout<<!test.empty()<<std::endl;
//empty()方法空为1,不空为0
for(auto &iter:test){
std::cout<<iter.first<<"|";
iter.second.show();//std::cout<<(void)<<std::endl;cout时不能出现这种情况
}
std::cout<<"test大小:"<<test.size()<<" test最大容量:"<<test.max_size()<<std::endl;
return 0;
}
//std::map::lower_bound(key值) 和 std::map::upper_bound(key值)
//low_bound和upper_bound返回iterator或const_iterator迭代器
//low_bound的first指向有key值的实例,second指向下一个
//low_bound的first指向有key值的实例,second指向上一个
//std::swap(对象1,对象2)两个必须都是同样类型,字面意思交换,内容交换
可能还有什么遗漏,希望大家可以直接去 map::map - C++ Reference学习