• 第九章 顺序容器


     

    code:

    /*
    
    第二部分 容器和算法
    
    第9章 顺序容器
    9.1 顺序容器的定义
    9.2 迭代器和迭代器范围
    9.3 顺序容器的操作
    9.4 vector容器的自增长
    9.5 容器的选用
    9.6 再谈string类型
    9.7 容器适配器
    小结
    
    
    
    第9章 顺序容器 263
    9.1 顺序容器的定义 264
    9.1.1 容器元素的初始化 265
    9.1.2 容器内元素的类型约束 267
    9.2 迭代器和迭代器范围 268
    9.2.1 迭代器范围 270
    9.2.2 使迭代器失效的容器操作 271
    9.3 顺序容器的操作 272
    9.3.1 容器定义的类型别名 272
    9.3.2 begin和end成员 273
    9.3.3 在顺序容器中添加元素 273
    9.3.4 关系操作符 277
    9.3.5 容器大小的操作 278
    9.3.6 访问元素 279
    9.3.7 删除元素 280
    9.3.8 赋值与swap 282
    9.4 vector容器的自增长 284
    9.5 容器的选用 287
    9.6 再谈string类型 289
    9.6.1 构造string对象的其他方法 290
    9.6.2 修改string对象的其他方法 292
    9.6.3 只适用于string类型的操作 293
    9.6.4 string类型的查找操作 295
    9.6.5 string对象的比较 298
    9.7 容器适配器 300
    9.7.1 栈适配器 301
    9.7.2 队列和优先级队列 302
    小结 303
    术语 303
    
    
    */
    
    
    
    
    //9.1 顺序容器的定义 ------------------------------------------------------------------------------------------------
    
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    #include "Sales_item.h"
    using namespace std;
    
    int main()
    {
      vector < string > svec; // empty vector that can hold strings
      list < int > ilist;     // empty list that can hold ints
      deque < Sales_item > items; // empty deque that holds Sales_items
      
      return 0;
    }
    
    
    // 初始化的各种方式
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    
    typedef int T;
    typedef vector<T> C;
    
    int main()
    {
      C a;
      C b(a);
      C c(a.begin(),a.begin());
      C d(2,3);
      C e(2);
      //cout << sizeof(C) << endl;
      
      return 0;
    }
    
    
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    
    int main()
    {
      vector<int> ivec(2,3);
      vector<int> ivec2(ivec);   // ok: ivec is vector<int>
      // list<int>   ilist(ivec);   // error: ivec is not list<int>
      // vector<double> dvec(ivec); // error: ivec holds int not double
      
      return 0;
    }
    
    
    //
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      string s("test");
      vector < string > svec(3, s);
      
      // initialize slist with copy of each element of svec
      list < string > slist(svec.begin(), svec.end());
      
      // find midpoint in the vector
      vector < string > ::iterator mid = svec.begin() + svec.size() / 2;
      // initialize front with first half of svec: The elements up to but not including *mid
      deque < string > front(svec.begin(), mid);
      // initialize back with second half of svec: The elements *mid through end of svec
      deque < string > back(mid, svec.end());
      
      return 0;
    }
    
    
    // in book
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      char *words[] = { "stately", "plump", "buck", "mulligan" };
      // calculate how many elements in words
      size_t words_size = sizeof(words) / sizeof(char*); // char* ?
      // use entire array to initialize words2
      list < string > words2(words, words + words_size);
      
      cout << sizeof(char*) << endl; // 指针长度,可计算出words数组中有几个元素(字符串),即数组长度
      
      return 0;
    }
    
    
    // in book
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      const list < int > ::size_type list_size = 64;
      list < string > slist(list_size, "eh?"); // 64 strings, each is eh?
      
      list < int > ilist(list_size); // 64 elements, each initialized to 0
      // svec has as many elements as the return value from get_word_count
      //extern unsigned get_word_count(const string &file_name);
      
      // vector < string > svec(get_word_count("Chimera"));
      string s("Chimera");
      vector<string> svec(s.size());
      return 0;
    }
    
    
    // in book
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      // note spacing: use ">>" not ">>" when specifying a container element type
      vector < vector < string >  > linesa; // vector of vectors
      
      vector < vector < string >  > linesb; // ok: space required between close >
      // vector < vector < string >> lines;c // error: >> treated as shift operator
      return 0;
    }
    
    
    
    //9.2 迭代器和迭代器范围 ------------------------------------------------------------------------------------------------
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector<int> vec(3,2);
      vector<int>::iterator iter = vec.begin() + vec.size()/2;
      cout << iter-vec.begin() << endl; // 
      
      // copy elements from vec into ilist
      list<int> ilist(vec.begin(), vec.end());
      list<int>::iterator liit;
      // liit=ilist.begin() + ilist.size()/2; // error: no addition on list iterators
    
      return 0;
    }
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector<int> vec(3,2);
      vector<int>::iterator first,last;
      first=vec.begin(); last=vec.end();
      while(first!=last)
        cout << *first++ << endl;
     
      return 0;
    }
    
    
    
    
    //9.3 顺序容器的操作 ------------------------------------------------------------------------------------------------
    
    
    
    // 9.3.1. 容器定义的类型别名
    
    // about , test const_iterator
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector<int> vec(3,2);
      vector<int>::const_iterator first,last; //const_iterator是指指向的容易不能改变
      first=vec.begin(); last=vec.end();      //但 const_iterator,比如first,可以改变
      while(first!=last)
        cout << *first++ << endl;
      first=vec.begin();
      // *first = 8; // error 因为first是 const_iterator
      return 0;
    }
    
    
    // 所有容器都定义的类型
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    
    int main()
    {
      typedef vector<int> VI;
      VI vec(3,2);  
      VI::size_type si;
      VI::iterator iter;
      VI::const_iterator citer;
      VI::reverse_iterator ri;
      VI::const_reverse_iterator cri;
      VI::difference_type dt;
      VI::value_type vt;
      /* 以下三个可能不是这样用,以后再看
      VI::const_value_type cvt;
      VI::reference ref;
      VI::const_reference cref;
      */
      return 0;
    }
    
    
    // 9.3.2. begin 和 end 成员
    
    // test rbegin rend...
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector<int> vec;
      for(int i=0; i<3; ++i)
        vec.push_back(i);
     
      vector<int>::iterator first,last;
      first=vec.begin(); last=vec.end();
      while(first!=last)
        cout << *first++ << endl;
    
      vector<int>::reverse_iterator rfirst,rlast;
      rfirst=vec.rbegin(); rlast=vec.rend(); // 用上了
      while(rfirst!=rlast) // 倒着输出
        cout << *rfirst++ << endl;  
     
      return 0;
    }
    
    /*
    这些操作返回什么类型取决于容器是否为 const。
    如果容器不是 const,则这些操作返回 iterator 或 reverse_iterator 类型。
    如果容器是 const,则其返回类型要加上 const_ 前缀,也就是 const_iterator 和 const_reverse_iterator 类型。
    */
    
    
    
    //9.3.3. 在顺序容器中添加元素
    
    // push_back
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector<string> vec;
      // read from standard input putting each word onto the end of container
      string text_word;
      while (cin >> text_word)
        //container.push_back(text_word);
        //The type of container can be any of list, vector, or deque
        vec.push_back(text_word);
      // 所有顺序容器都支持 push_back 操作
    
      return 0;
    }
    
    
    
    // 除了 push_back 运算,list 和 deque 容器类型还提供了类似的操作:push_front
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      list < int > ilist;
      // add elements at the end of ilist
      for(size_t ix = 0; ix != 4; ++ix)
        ilist.push_back(ix);
      // add elements to the start of ilist
      for(size_t ix = 0; ix != 4; ++ix)
        ilist.push_front(ix);
      // put out
      list<int>::iterator it=ilist.begin();
      while( it!=ilist.end() )
        cout << *it++ << endl;
    
      return 0;
    }
    
    
    // insert 一个元素
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector < string > svec;
      list < string > slist;
      string spouse("Beth");
      // equivalent to calling slist.push_front (spouse);
      slist.insert(slist.begin(), spouse);
      
      // no push_front on vector but we can insert before begin()
      // warning: inserting anywhere but at the end of a vector is an expensive operation
      // 警告:除了在vector的尾部进行插入,否则在vector的任何地方插入,都是非常昂贵的操作
      svec.insert(svec.begin(), spouse); // 昂贵
      svec.insert(svec.end(),spouse); // 便宜
      return 0;
    }
    
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      list<string> lst;
      list<string>::iterator iter = lst.begin();
      string word;
      while (cin >> word)
        iter = lst.insert(iter, word); // same as calling push_front
      //insert 返回指向新添加元素的迭代器
    
      // iter 在 begin() 位置
      while( iter!=lst.end() )
        cout << *iter++ << endl;
    
      return 0;
    }
    
    
    
    // 插入数个元素
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      list<string> lst;
      list<string>::iterator iter = lst.begin();
      string word;
      while (cin >> word)
        iter = lst.insert(iter, word); // same as calling push_front
      //insert 返回指向新添加元素的迭代器
    
      // iter 在 begin() 位置
      while( iter!=lst.end() )
        cout << *iter++ << endl;
      cout << endl << endl;
    
      lst.insert(lst.end(),10,"Anna"); // 返回void,相当于一个过程
      iter=lst.begin();
      while( iter!=lst.end() )
        cout << *iter++ << endl;  
    
      return 0;
    }
    
    
    // 从另外容器(数组)中,取一段数据插入
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      list<string> lst;
      list<string>::iterator iter;
    
      lst.insert(lst.end(),3,"Anna"); // 返回void,相当于一个过程
      iter=lst.begin();
      while( iter!=lst.end() )
        cout << *iter++ << endl;
    
      string sarray[4] = {"quasi", "simba", "frollo", "scar"};
      // insert all the elements in sarray at end of slist
      lst.insert(lst.end(), sarray, sarray+4);
      iter=lst.begin();
      cout << endl;
      while( iter!=lst.end() )
        cout << *iter++ << endl;    
    
      iter=lst.begin();
      // insert last two elements of sarray before slist_iter
      lst.insert(iter, sarray+2, sarray+4);
      cout << endl;  
      iter=lst.begin();
      while( iter!=lst.end() )
        cout << *iter++ << endl;  
    
      return 0;
    }
    
    
    // 迭代器出问题。书中分析错误,与last无关,改成v.end()也木用。
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector < int > v(3,2);
      vector < int > ::iterator first = v.begin(), last = v.end(), iter;
      // cache end iterator
      // diaster: behavior of this loop is undefined
      while(first != last)
      {
        // do some processing
        // insert new value and reassign first, which otherwise would be invalid
        first = v.insert(first, 42);
        cout << *first << ' ';
        ++first; // advance first just past the element we added
        } // 不是last的问题,是insert的返回first问题。first永远到不到last
          // 书中分析错误,与last无关
      iter=v.begin();
      while( iter!=v.end() )
        cout << *iter++ << endl;  
      
      return 0;
    }
    
    
    // 9.3.4. 关系操作符
    
    // 9.3.5. 容器大小的操作
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector < int > v;
      cout << v.empty() << endl;
      vector < int > va(3,2); // 2 2 2
      cout << va.size() << endl;
      cout << va.max_size() << endl;
      va.resize(5); // 2 2 2 0 0
      va.resize(8,8);
      
      vector<int>::iterator iter=va.begin();
      while( iter!=va.end() )
        cout << *iter++ << endl;  
    
      return 0;
    }
    
    
    
    // 9.3.6. 访问元素
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      list < int > ilist(3,2);
      // check that there are elements before dereferencing an iterator
      // or calling front or back
      if(!ilist.empty())
      {
        // val and val2 refer to the same element
        list < int > ::reference val =  *ilist.begin();
        list < int > ::reference val2 = ilist.front();
        cout << val << ' ' << val2 << endl;
        
        // last and last2 refer to the same element
        list < int > ::reference last =  *--ilist.end();
        list < int > ::reference last2 = ilist.back();
        cout << last << ' ' << last2 << endl;
      }
      return 0;
    }
    
    
    // *** [], at() 只适用于 vector 和 deque 容器 ***
    
    
    // 9.3.7. 删除元素
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector<int> v(3,2);
      v.erase(v.begin(),v.begin()+1); //注意,只删除了第一个,第二个木有删除
      cout << v.size() << endl << endl;
      vector<int>::iterator it=v.begin();
      while(it!=v.end())
        cout << *it++ << endl;
    
      return 0;
    }
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector<int> v(3,2);
      v.erase(v.begin(),v.begin()+1); //注意,只删除了第一个,第二个木有删除
      cout << v.size() << endl << endl;
      vector<int>::iterator it=v.begin();
      while(it!=v.end())
        cout << *it++ << endl;
      
      cout << endl;
      v.pop_back(); // 删除了最后一个。删除第一个:pop_front()只能用于list和deque
      cout << v.size() << endl;
      v.clear(); // 全部删除,都木有了
      cout << v.size() << endl;
    
      return 0;
    }
    
    
    // 9.3.8. 赋值与 swap
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector<int> c1(3,2),c2(5,3);
      //c1 = c2; // replace contents of c1 with a copy of elements in c2
      // equivalent operation using erase and insert
      c1.erase(c1.begin(), c1.end()); // delete all elements in c1
      c1.insert(c1.begin(), c2.begin(), c2.end()); // insert c2
      cout << (c1==c2) << endl;
      c1=c2;
      cout << (c1==c2) << endl;
      return 0;
    }
    
    
    
    // swap, assign
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector<int> c1(8,2),c2(5,3);
      cout << c1.size() << ' ' << c2.size() << endl;
      c1.swap(c2); // 估计只是交换了指针
      cout << c1.size() << ' ' << c2.size() << endl;
    
      c1.assign(c2.begin(),c2.begin()+3); // 复制到第二个参数的前一个位置
      c2.assign(c1.begin(),c1.begin()+1);
      cout << c1.size() << ' ' << c2.size() << endl;  
      
      c1.assign(8,2);
      c2.assign(5,3);
      cout << c1.size() << ' ' << c2.size() << endl;  
      
      return 0;
    }
    
    
    
    
    //9.4 vector容器的自增长 ------------------------------------------------------------------------------------------------
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector < int > ivec;
      // size should be zero; capacity is implementation defined
      cout << "ivec: size: " << ivec.size() 
           << " capacity: " << ivec.capacity() << endl;
      
      // give ivec 24 elements
      for(vector < int > ::size_type ix = 0; ix != 24; ++ix)
        ivec.push_back(ix);
      // size should be 24; capacity will be >= 24 and is implementation defined
      cout << "ivec: size: " << ivec.size() 
           << " capacity: " << ivec.capacity() << endl;
    
      return 0;
    }
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    int main()
    {
      vector < int > ivec;
      // size should be zero; capacity is implementation defined
      cout << "ivec: size: " << ivec.size() 
           << " capacity: " << ivec.capacity() << endl;
      
      // give ivec 24 elements
      for(vector < int > ::size_type ix = 0; ix != 24; ++ix)
        ivec.push_back(ix);
      // size should be 24; capacity will be >= 24 and is implementation defined
      ivec.reserve(50); //// sets capacity to at least 50; might be more
      cout << "ivec: size: " << ivec.size() 
           << " capacity: " << ivec.capacity() << endl;
    
      // add elements to use up the excess capacity
      while (ivec.size() != ivec.capacity())
        ivec.push_back(0);
      // size should be 50; capacity should be unchanged
      cout << "ivec: size: " << ivec.size()
           << " capacity: "  << ivec.capacity() << endl;
    
      ivec.push_back(42); // add one more element
      // size should be 51; capacity will be >= 51 and is implementation defined
      cout << "ivec: size: " << ivec.size()
           << " capacity: "  << ivec.capacity() << endl;
    
      return 0;
    }
    
    
    
    //9.5 容器的选用 ------------------------------------------------------------------------------------------------
    
    
    // ***通常来说,除非找到选择使用其他容器的更好理由,否则 vector 容器都是最佳选择。***
    
    
    //9.6 再谈string类型 ------------------------------------------------------------------------------------------------
    // 9.6.1. 构造 string 对象的其他方法
    // 9.6.2. 修改 string 对象的其他方法
    // 9.6.3. 只适用于 string 类型的操作
    // 9.6.4. string 类型的查找操作
    // 9.6.5. 字符串比较
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <list>
    #include <deque>
    using namespace std;
    
    int main()
    {
      string s("Hiya!");
      string::iterator iter = s.begin();
      while (iter != s.end())
        cout << *iter++ << endl; // postfix increment: print old value
    
      return 0;
    }
    
    
    // 9.6.1. 构造 string 对象的其他方法
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
      string s1;           // s1 is the empty string
      string s2(5, 'a');   // s2 == "aaaaa"
      string s3(s2);       // s3 is a copy of s2
      string s4(s3.begin(), s3.begin() + s3.size() / 2); // s4 == "aa"
    
      return 0;
    }
    
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
      char *cp = "Hiya"; // null-terminated array
      char c_array[] = "World!!!!"; // null-terminated
      char no_null[] = {'H', 'i'};  // not null-terminated
      string s1(cp); // s1 == "Hiya"
      string s2(c_array, 5); // s2 == "World"
      string s3(c_array + 5, 4); // s3 == "!!!!"
      string s4(no_null); // runtime error: no_null not null-terminated
      string s5(no_null, 2); // ok: s5 == "Hi"
      string s6(s1, 2);    // s6 == "ya"
      string s7(s1, 0, 2); // s7 == "Hi"
      string s8(s1, 0, 8); // s8 == "Hiya"
      s8.assign(5,'a'); cout << s8 << endl;
      s8.resize(9,'b'); cout << s8 << endl;
    
      return 0;
    }
    
    
    // 9.6.2. 修改 string 对象的其他方法
    
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
      string s("hello world!");
      s.erase(s.size()-5, 5); // erase last five characters from s
      cout << s << endl;
      s.insert(s.size(), 5, '!'); // insert five exclamation points at end of s
      cout << s << endl;
      
      return 0;
    }
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
      char *cp = "Stately plump Buck";
      string s,s2;
      s.assign(cp, 7); // s == "Stately"
      s.insert(s.size(), cp + 7); // s == "Stately plump Buck"
      
      s = "some string.";
      s2 = "some other string.";
      // 3 equivalent ways to insert all the characters from s2 at beginning of s
      // insert iterator range before s.begin()
      s.insert(s.begin(), s2.begin(), s2.end());
      cout << s << endl;  
      
      // insert copy of s2 before position 0 in s
      s.insert(0, s2);
      cout << s << endl;
      
      // insert s2.size() characters from s2 starting at s2[0] before s[0]
      s.insert(0, s2, 0, s2.size());
      cout << s << endl;
    
      return 0;
    }
    
    
    
    // 9.6.3. 只适用于 string 类型的操作
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    void print(string s)
    {
      cout << s << endl;
    }
    
    int main()
    {
      string s("hello world");
      // return substring of 5 characters starting at position 6
      string s2 = s.substr(6, 5);   // s2 = world
      print(s2);
      
      // return substring from position 6 to the end of s
      string s3 = s.substr(6);      // s3 = world
      print(s3);
      
      return 0;
    }
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    void print(string s)
    {
      cout << s << endl;
    }
    
    int main()
    {
       string s("C++ Primer");        // initialize s to "C++ Primer"
       s.append(" 3rd Ed.");          // s == "C++ Primer 3rd Ed."
       print(s);
      
       // equivalent to s.append(" 3rd Ed.")
       s.insert(s.size(), " 3rd Ed.");
       print(s);
      
      return 0;
    }
    
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    void print(string s)
    {
      cout << s << endl;
    }
    
    int main()
    {
      string s("C++ Primer 3rd Ed.");  
      // starting at position 11, erase 3 characters and then insert "4th"
      s.replace(11, 3, "4th");          // s == "C++ Primer 4th Ed."
      print(s);
      
      // equivalent way to replace "3rd" by "4th"
      s.erase(11, 3);                   // s == "C++ Primer Ed."
      s.insert(11, "4th");              // s == "C++ Primer 4th Ed."
      print(s);
      
      s.replace(11, 3, "Fourth");       // s == "C++ Primer Fourth Ed."
      print(s);
      
      return 0;
    }
    
    
    
    
    // 9.6.4. string 类型的查找操作
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    void print(string::size_type n)
    {
      cout << n << endl;
    }
    
    int main()
    {
      cout << string::npos << endl; // 32位全是1,无符号
      cout << 0xFFFFFFFF << endl;
      
      string name("AnnaBelle");
      string::size_type pos1 = name.find("Anna"); // pos1 == 0
      print(pos1);
      
      string lowercase("annabelle");
      pos1 = lowercase.find("Anna"); // pos1 == npos
      print(pos1);
      
      string numerics("0123456789");
      name="r2d2";  // 下句:在name中查找numerics中任意字符的第一次出现,这里即第一次出现数字
      string::size_type pos = name.find_first_of(numerics);
      cout << "found number at index: " << pos
           << " element is "  << name[pos] << endl;
      
      cout << endl;
      pos = 0;
      // each trip reset pos to the next instance in name
      while ((pos = name.find_first_of(numerics, pos))!= string::npos) {
        cout << "found number at index: " << pos // 上句,pos既是参数,又是返回值
             << " element is " << name[pos] << endl;
        ++pos; // move to the next character
      }
    
      return 0;
    }
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    void print(string::size_type n)
    {
      cout << n << endl;
    }
    int main()
    {
      string numbers("0123456789");
      string dept("03714p3");
      
      // returns 5, which is the index to the character 'p'
      string::size_type pos = dept.find_first_not_of(numbers);
      print(pos);
      
      string river("Mississippi");
      string::size_type first_pos = river.find("is"); // returns 1
      print(first_pos);
      string::size_type last_pos = river.rfind("is"); // returns 4
      print(last_pos);
      
      return 0;
    }
    
    
    
    // 9.6.5. 字符串比较
    
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
      string cobol_program_crash("abend");
      string cplus_program_crash("abort");
      
      // returns a negative value
      cobol_program_crash.compare(cplus_program_crash);
      
      // returns a positive value
      cplus_program_crash.compare(cobol_program_crash);
      
      char second_ed[] = "C++ Primer, 2nd Edition";
      string third_ed("C++ Primer, 3rd Edition");
      string fourth_ed("C++ Primer, 4th Edition");
      
      // compares C++ library string to C-style string
      fourth_ed.compare(second_ed); // ok, second_ed is null-terminated
      
      // compare substrings of fourth_ed and third_ed
      fourth_ed.compare(fourth_ed.find("4th"), 3, third_ed, third_ed.find("3rd"), 3);
      
      return 0;
    }
    
    
    
    
    //9.7 容器适配器 ------------------------------------------------------------------------------------------------
    
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <stack>
    #include <queue>
    using namespace std;
    
    int main()
    {
      vector<string> svec;
      
      // empty stack implemented on top of vector
      stack< string, vector<string> > str_stk;
    
      // str_stk2 is implemented on top of vector and holds a copy of svec
      stack<string, vector<string> > str_stk2(svec);
      
      return 0;
    }
    
    
    // 默认的 stack 和 queue 都基于 deque 容器实现,而 priority_queue 则在 vector 容器上实现。
    // 演示进栈、出栈
    #include <iostream>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <stack>
    #include <queue>
    using namespace std;
    int main()
    {
      // number of elements we'll put in our stack
      const stack < int > ::size_type stk_size = 10;
      stack < int > intStack; // empty stack
      
      // fill up the stack
      int ix = 0;
      // use postfix increment; want to push old value onto intStack  
      while(intStack.size() != stk_size) intStack.push(ix++);
      // intStack holds 0...9 inclusive
    
      int error_cnt = 0;
      // look at each value and pop it off the stack
      while(intStack.empty() == false)
      {
        int value = intStack.top();
        // read the top element of the stack
        if(value != --ix)
        {
          cerr << "oops! expected " << ix << " received " << value << endl;
          ++error_cnt;
        }
        intStack.pop(); // pop the top element, and repeat
      }
      cout << "Our program ran with " << error_cnt << " errors!" << endl;
      return 0;
    }
    
    
    // 优先队列中,内部的数据结构是什么?
    // 应该是堆吧。出队列时,可以看到,从大到小排好了。
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <stack>
    #include <queue>
    using namespace std;
    int main()
    {
      priority_queue<int> pq;
      int n(10);
      srand( time(0) );
      while(n--) pq.push( rand()%20 );
      // cout << pq[3] << endl; // error:队列不可以随机访问
      while( !pq.empty() ) {
        cout << pq.top() << endl;
        pq.pop();
      }
      return 0;
    }

    TOP

  • 相关阅读:
    Alice and Bob 要用到辗转相减
    Java经典设计模式
    设计模式---创建类---建造者模式
    luogu4267 TamingtheHerd (dp)
    nowcoder172C 保护 (倍增lca+dfs序+主席树)
    nowcoder172A 中位数 (二分答案)
    bzoj4985 评分 (二分答案+dp)
    luogu4269 Snow Boots G (并查集)
    luogu4268 Directory Traversal (dfs)
    bzoj1001/luogu4001 狼抓兔子 (最小割/平面图最小割转对偶图最短路)
  • 原文地址:https://www.cnblogs.com/xin-le/p/4088107.html
Copyright © 2020-2023  润新知