C++学习笔记1

将最近工做中,方向转换比较快,常常一个季度作这个,一个季度作那个,遇到一些经常使用的C++语法,而记性不太好常常忘记,整理一下笔记,将一些本身喜欢用的语法记录下来,方便本身查阅。ios

 

 

map用法:
std::map<int, ststructInfo>::iterator iter = vec.begin();
for( , iter != vec.end(), iter++) {
  if(flage == iter->second.flage){
    pLarge = iter->second;
  }
}
std:map<int,string> personnel;
这样就定义了一个用int做为索引,并拥有相关联的指向string的指针.
为了使用方便,能够对模板类进行一下类型定义:json

typedef map<int,CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;
//数据的插入--第一种:用insert函数插入pair数据  
#include <map>  
#include <string>  
#include <iostream>   
using namespace std;  
int main()   
{  
    map<int, string> mapStudent;  
    mapStudent.insert(pair<int, string>(1, "student_one"));  
    mapStudent.insert(pair<int, string>(2, "student_two"));    
    map<int, string>::iterator iter;  
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
       cout<<iter->first<<' '<<iter->second<<endl;   
}  
//第二种:用insert函数插入value_type数据,下面举例说明  
#include <map>  
#include <string>   
#include <iostream>   
using namespace std;  
int main()   
{  
    map<int, string> mapStudent;  
    mapStudent.insert(map<int, string>::value_type (1, "student_one")); 
    mapStudent.insert(map<int, string>::value_type (2, "student_two"));     
    map<int, string>::iterator iter;   
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)   
       cout<<iter->first<<' '<<iter->second<<endl;   
}  
//第三种:用数组方式插入数据,下面举例说明    
#include <map>    
#include <string>   
#include <iostream>   
using namespace std;   
int main()   
{   
    map<int, string> mapStudent;  
    mapStudent[1] = "student_one";   
    mapStudent[2] = "student_two";  
    map<int, string>::iterator iter;   
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)  
        cout<<iter->first<<' '<<iter->second<<endl;  
}  数组


  以上三种用法,虽然均可以实现数据的插入,可是它们是有区别的,固然了第一种和第二种在效果上是完成同样的,用insert函数插入数据,在数据的 插入上涉及到集合的惟一性这个概念,即当map中有这个关键字时,insert操做是插入数据不了的,可是用数组方式就不一样了,它能够覆盖之前该关键字对 应的值。函数

map临时保存数据时,调用通常设置保护锁,保证数据准确性:this

 VOS_Mutex *m_mapIdMutex;

 

无序容器

C++11 引入了两组无序容器:
std::unordered_map/std::unordered_multimap 和 std::unordered_set/std::unordered_multiset。spa

无序容器中的元素是不进行排序的,内部经过 Hash 表实现,插入和搜索元素的平均复杂度为 O(constant)。指针

具体使用例子:排序

    m_msgHandlerMap["MariaWay"] = std::bind(&CSession::HandleCmd, this, 
        std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
class CSession{
public:
  int handleCmd(const Json::Value &root,  pLens &pt,  Json::Value &jsonRsp )
 
private:
  std::unordered_map<std::string, std::function<int(const Json::Value &root,  pLens &pt,  Json::Value &jsonRsp)>> m_msgHandlerMap;  //使用C++11特性模板,容器模板
}
相关文章
相关标签/搜索