zoukankan      html  css  js  c++  java
  • C++ vector 使用笔记

    map 插入 vector 

    #include <string>   
    
    #include <iostream>   
    
    #include <algorithm>   
    
    #include <map>   
    
    #include <vector>   
    
    using namespace std;   
    
    typedef map<string, string> STRING2STRING;   
    
    typedef std::map<string, vector<string > > STRING2VECTOR;   
    
     
    
    int main()   
    {   
    
        std::map<string, string > map_test;   
    
        map_test.insert(STRING2STRING::value_type("2001", "test1"));   
        map_test.insert(STRING2STRING::value_type("2005", "test5"));   
    
        map<string, string>::const_iterator map_conitor = map_test.begin();   
        for(; map_conitor!= map_test.end(); map_conitor++)   
        {   
    
            cout<<map_conitor->first<<"  "<<map_conitor->second<<endl;      
        }   
    
    
        
    
        map<int, vector<string> > m_map; 
    
    
        vector<string> m_vec;
        m_vec.resize(2);
    
        m_vec[0] = "aaaaa";
        m_vec[1] = "bbbbb";
        // m_vec.clear();
        // cout<<m_vec[0]<<endl;
    
    
        m_map[0] = m_vec;
        if(m_map[0].empty()) { cout<<"not push_back can not assignment"<<endl;}
       
        cout<<m_map[0][1]<<endl;
    
    
    
    
        //  push_back 方式
        vector<string> m_vecPush;
        m_vecPush = m_map[0];
        cout<<"assignment m_vecPush:"<<m_vecPush[0] <<endl;
    
    
    
        map<int, vector<string> > m_mapPush;
        m_vecPush.resize(2);
    
        m_vecPush.push_back("aaaaa");
        m_vecPush.push_back("bbbbb");
    
        
        //  m_mapPush[0] = m_vecPush;   与 m_mapPush.insert 等价
        m_mapPush.insert(pair<int,vector<string> >(0,m_vecPush));  
    
    
    
    
       if(m_mapPush[0].empty()) { cout<<"push_back can not assignment"<<endl;}
        cout<<m_mapPush[0][1]<<endl;
    
    
    
        // m_mapPush[dev_id].empty()  与  m_mapPush.find(dev_id) == m_mapPush.end() 类似
        if(m_mapPush[2].empty()) {
            cout<<"m_mapPush empty way  not exits"<<endl;
        }
    
        if(m_mapPush.find(1) == m_mapPush.end()) {
            cout<<"m_mapPush find way  not exits"<<endl;
        }
    
    
    
    
        /////////////////////////// vector 清空元素  ///////////////////////////////
         // 清空元素,不回收空间
        m_vecPush.clear(); 
        cout<<"clear vector size:"<<m_vecPush.capacity()<<endl;
        // 清空元素并释放空间
        m_vecPush.clear(); 
        vector<string>().swap(m_vecPush);
        cout<<"swap vector size:"<<m_vecPush.capacity()<<endl;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     }

     之前在开发板上使用

    m_vec[0] = "aaaaa"; 这种方式,然后用 m_map[0] = m_vec; 发现m_vec赋值不成功。要用m_vec.push_back("aaaa")这种方式才能赋值给map。 但在台式linux上不存在这种情况。

  • 相关阅读:
    java接口对入参的判断校验
    sqlyog使用技巧
    mysql 数据库的表中复制一条数据并添加到该表中
    union all ,union 注意事项,查询结果集中的字段名称顺序必须一致
    IDEA连接mysq数据库,其实很简单
    git版本回退、git远程分支管理、git本地分支管理、git生产代码bug修复
    Vue上拉加载下拉刷新---vue-easyrefresh
    Flutter上拉加载下拉刷新---flutter_easyrefresh
    vue-cli webpack多Html页面的配置(附框架vue-webpack-multipage实例)
    Qt使用镜像源快速安装与更新
  • 原文地址:https://www.cnblogs.com/hzijone/p/6600301.html
Copyright © 2011-2022 走看看