MAP赋值和插入,对于相同ID的处理方式不同,前者为替换 后者为插入失败

 

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
   map<int, string> mapStudent;
   pair<map<int, string>::iterator, bool> Insert_Pair;
   mapStudent[1] = "student_one";
   mapStudent[1] = "student_one2";   
   
   cout << "====================MAP  赋值测试=====================\n" <<endl ;
   map<int, string>::iterator  iter;
   for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
   {
       cout<<iter->first<<" "<<iter->second<< endl;
   }
   
   cout << "====================MAP  INSERT测试=====================\n" <<endl ;
   Insert_Pair = mapStudent.insert(pair<int, string>(2, "student_2"));
   if(Insert_Pair.second == true)
   {
          cout<<"Insert Successfully"<<endl;
   }
   else
   {
          cout<<"Insert Failure"<<endl;
   }
   Insert_Pair = mapStudent.insert(pair<int, string>(2, "student_2222"));
   if(Insert_Pair.second == true)
   {
          cout<<"Insert Successfully"<<endl;
   }
   else
   {
          cout<<"Insert Failure"<<endl;
   }
   
   for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
   {
       cout<<iter->first<<" "<<iter->second<< endl;
   }
}
语艺杂谈1 – MAP赋值与插入 
 

相关文章:

  • 2019-01-11
  • 2021-07-15
  • 2022-01-08
  • 2022-12-23
  • 2021-11-09
  • 2022-03-05
  • 2021-07-24
  • 2021-04-12
猜你喜欢
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2022-01-11
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案