日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关联容器的插入操作简单举例

發布時間:2024/4/18 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关联容器的插入操作简单举例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

set.insert()

map.insert()

共有三種方法,返回值各不相同,注意!

Extends the container by inserting new elements, effectively increasing the container?size?by the number of elements inserted.

Because element keys in a?map?are unique, the insertion operation checks whether each inserted element has a key equivalent to the one of an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).

For a similar container allowing for duplicate elements, see?multimap.

An alternative way to insert elements in a?map?is by using member function?map::operator[].

Internally,?map?containers keep all their elements sorted by their key following the criterion specified by its?comparison object. The elements are always inserted in its respective position following this ordering.

The parameters determine how many elements are inserted and to which values they are initialized:

?

/*single element (1)*/ pair<iterator,bool> insert (const value_type& val);/*with hint (2) */ iterator insert (iterator position, const value_type& val);/*range (3) */ template <class InputIterator>void insert (InputIterator first, InputIterator last); #include <string> #include <iostream> #include <list> #include <vector> #include <set> #include <map> #include <fstream> using namespace std; void print(const set<int> &); void print(const map<string,size_t> &); int main() { //c.insert(v) //c.emplace(v) //*single element (1)*/ //function 's return type is: //pair<iterator,bool> insert (const value_type& val); set<int> s1; map<string,size_t> m1; pair<set<int>::iterator,bool> it1 =s1.insert(1); cout << *it1.first << endl; cout << it1.second << endl; s1.emplace(2); m1.insert({"abc",3});/* this is wrong,why? m1.emplace({"abcd",5}); */ print(s1); print(m1);//c.insert(p,v) //c.emplace(p,v) // /*with hint (2) */ //iterator insert (iterator position, const value_type& val); set<int>::iterator it2 = s1.insert(s1.begin(),123); m1.insert({"abcd",3});//c.insert(b,e) //c.insert(il) /*range (3) */ //template <class InputIterator> // void insert (InputIterator first, InputIterator last);s1.insert({111,222,333,444}); m1.insert({{"aaaa",1},{"bbbb",2},{"cccc",3},{"dddd",6}});return 0;}void print(const map<string,size_t> &m) { map<string,size_t>::const_iterator it; cout << "-------------------------begin----------------------------"<< endl; for(it = m.begin();it != m.end();++ it){cout << it->first << endl;cout << it->second <<endl;} cout << "----------------------------end-----------------------------"<<endl; } void print(const set<int> &s) { cout << "------------------------set begin------------------------------"<<endl; set<int>::const_iterator it; for(it = s.begin();it != s.end(); ++it)cout << *it << endl; cout << "---------------------------end---------------------------" <<endl; }

?

總結

以上是生活随笔為你收集整理的关联容器的插入操作简单举例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。