zoukankan      html  css  js  c++  java
  • cb09a_c++_顺序容器的操作2-在顺序容器中添加元素_插入数据

    cb09a_c++_顺序容器的操作2
    在顺序容器中添加元素
    vector不能向前插入数据,list可以用insert
    c.push_back(t);
    c.push_front(t);
    c.insert(p,t);在迭代器p,插入t
    c.insert(p,n,t);在迭代器p,插入n个t
    c.insert(p,b,e);把迭代器b,e之间的数据,插入到迭代器p指向的位置。
    容器元素都是副本
    添加元素可能会使迭代器失效
    避免存储end操作返回的迭代器

    welcome to discuss
    txwtech@163.com

    //vector<int>::iterator last = ivec2a.end();//不要这样写,不要提前保存。
    直接在某个地方用:ivec2a.end();
    while(first!=ivec2a.end())//这样既就ok咯。
    whiel(first!=last)//这样不好。不建议
    while(it!=)
    string sarray[4] = {"qusai","simba","frollo","scar"};//字符串数组
    const char *str[]= { "qusai","simba","frollo","scar" };//字符串数组

      1 /*cb09a_c++_顺序容器的操作2
      2 在顺序容器中添加元素
      3 vector不能向前插入数据,list可以用insert
      4 c.push_back(t);
      5 c.push_front(t);
      6 c.insert(p,t);在迭代器p,插入t
      7 c.insert(p,n,t);在迭代器p,插入n个t
      8 c.insert(p,b,e);把迭代器b,e之间的数据,插入到迭代器p指向的位置。
      9 容器元素都是副本
     10 添加元素可能会使迭代器失效
     11 避免存储end操作返回的迭代器
     12 
     13 welcome to discuss
     14 txwtech@163.com
     15 
     16 //vector<int>::iterator last = ivec2a.end();//不要这样写,不要提前保存。
     17 直接在某个地方用:ivec2a.end();
     18 while(first!=ivec2a.end())//这样既就ok咯。
     19 whiel(first!=last)//这样不好。不建议
     20 while(it!=)
     21 string sarray[4] = {"qusai","simba","frollo","scar"};//字符串数组
     22 const char *str[]= { "qusai","simba","frollo","scar" };//字符串数组
     23 */
     24 
     25 #include <iostream>
     26 #include <vector>
     27 #include <list>
     28 #include <deque>
     29 #include <string>
     30 
     31 using namespace std;
     32 int main()
     33 {
     34     vector<string> svec;
     35     list<string> slist;
     36     deque<string> sdeq;
     37 
     38     svec.push_back("Bill");
     39     svec.push_back("Tom");
     40     svec.push_back("Mary");
     41 
     42     
     43     slist.push_back("Bill");
     44     slist.push_back("Tom");
     45     slist.push_back("Mary");
     46 
     47     sdeq.push_back("Bill");
     48     sdeq.push_back("Tom");
     49     sdeq.push_back("Mary");
     50 
     51     slist.push_front("Primer");
     52     slist.push_front("C++");
     53 
     54     sdeq.push_front("Primer");
     55     sdeq.push_front("C++");
     56 
     57     for (list<string>::iterator iter = slist.begin();
     58         iter != slist.end(); ++iter)
     59         cout << "slist data: " << *iter << " " << endl;;
     60     cout << endl;
     61 
     62     list<string>::iterator it = slist.begin();
     63     cout << *it << endl;
     64     ++it;
     65     cout << *it << endl;
     66 
     67     //slist插入数据,迭代器所指向的数据的前面
     68     slist.insert(it,"hello");
     69     for (list<string>::iterator iter = slist.begin();
     70         iter != slist.end(); ++iter)
     71         cout << "slist data: " << *iter << " " << endl;;
     72     cout << endl;
     73 
     74     it = slist.begin();
     75     ++it;
     76     ++it;
     77     ++it;
     78     cout << *it << endl;//显示bill
     79 
     80     slist.insert(it, 10, "hi");//bill前面插入10个hi.
     81 
     82     string sarray[4] = {"qusai","simba","frollo","scar"};//字符串数组
     83     cout << sarray[0] << endl;
     84     const char *str[]= { "qusai","simba","frollo","scar" };//字符串数组
     85     cout << str[1] << endl;
     86 
     87     it = slist.begin();
     88     cout << *it << endl;//显示c++
     89     ++it;
     90     cout << *it << endl;//显示hello
     91 
     92     slist.insert(it, sarray + 1, sarray + 4);//把"simba","frollo","scar"插入到hello前面
     93 
     94     cout << "显示插入后的数据: " << endl;
     95     for (list<string>::iterator iter = slist.begin();
     96         iter != slist.end(); ++iter)
     97         cout << "slist data: " << *iter << " " << endl;;
     98     cout << endl;
     99 
    100 
    101     //vector<int> ivec2a;
    102     list<int> ivec2a;
    103     ivec2a.push_back(10);
    104     ivec2a.push_back(20);
    105     ivec2a.push_back(30);
    106 
    107     list<int>::iterator first = ivec2a.begin();
    108     //vector<int>::iterator last = ivec2a.end();//不能这样写,不能提前保存。
    109     //while (first != last)//不能这样写,不能提前保存。因为end在变化
    110     while(first!=ivec2a.end())//这样既就ok咯。
    111     {
    112         ivec2a.insert(first,32);
    113         ++first;
    114     }
    115     for(list<int>::iterator i=ivec2a.begin();
    116         i!=ivec2a.end();++i)
    117     { 
    118         cout << *i << endl;
    119     }
    120 
    121 
    122     return 0;
    123 }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    HDU 5918 SequenceI (2016 CCPC长春站 KMP模版变形)
    HDU 4585 Shaolin (set的应用)
    HDU 4329 MAP(stringstream的用法)
    CodeForces 698B Fix a Tree (并查集应用)
    UVALive 2520 Holedox Moving(BFS+状态压缩)
    UVA
    毛竹
    kmp
    博弈论
    最长回文子串
  • 原文地址:https://www.cnblogs.com/txwtech/p/12312276.html
Copyright © 2011-2022 走看看