C++ vector 使用筆記

來源:互聯網
上載者:User

標籤:val   names   pair   存在   clu   aaaaa   pre   ng2   its   

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上不存在這種情況。

 

C++ vector 使用筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.