Chinaunix首页 | 论坛 | 博客
  • 博客访问: 246883
  • 博文数量: 108
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 314
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-29 10:58
文章分类

全部博文(108)

文章存档

2015年(20)

2014年(88)

我的朋友

分类: 嵌入式

2014-05-06 21:29:24

标准库类型(二)

--vector类型


引子:

    vector是同一种类型的对象的集合,每个对象都有一个对应的整数索引值。和string对象一样,标准库将负责管理与存储元素相关的内存。

    我们将vector称之为容器,一个容器中的所有对象都必须是同一类型的!


  1. #include   
  2. using std::vector;  


【模板】

    vector就是一个类模板,使用模板可以编写一个类定义或函数定义,而用于多个不同的数据类型!

    但是,声明从类模板产生的某种类型的对象,需要提供附加信息。如:

       vector并不是一种数据类型,而vector,vector都是数据类型!


正文:

1vector对象的定义和初始化

  1. //vector的四种初始化方式  
  2. vector v1;  
  3. vector v2(v1);  
  4. vector v3(n,i);  
  5. vector v4(n);  

示例:

  1. vector<int> ivec1;  
  2. vector<int> ivec2(ivec1);  
  3. /* 
  4. *ERROR 
  5. *vector strVec(ivec2); 
  6. */  
  7.   
  8. vector<int> ivec3(10,-1);  
  9. vector strVec(10,"HI");  

2vector对象的值初始化

    1)如果vector对象保存的是内置数据类型(如:int),那么标准库将用0值创建元素初始化式。

    2)如果vector保存的是含有构造函数的类类型的元素,那么标准库将用该类型的默认构造函数创建元素初始化式。

   *3)如果vector保存的类类型元素没有默认构造函数,程序员就不能仅提供元素个数,还要提供初始值。


3vector对象的动态增长

    因为vector的增长效率非常高,所以,当元素值已知时,最好是通过动态的向它添加元素来让他“成长^_^”.

P79关键概念:vector对象的动态增长非常C/Java及其他程序员一读,推荐】


  1. //P80 习题3.11 下面语句正确or错误?  
  2.     vectorint>> ivec;           //在C++11中正确,在C++98/03中错误  
  3.     vector< vector<int> > ivec;             //正确  
  4.     vector svec(10,"NULL");                   //正确  


4vector对象的size

    成员函数size返回相应的vector类定义的size_type的值。

  1. vector<int>::size_type length = st.size();    //正确  
  2. vector::size_type lenth;            //错误  

5push_back()操作接受一个元素值。

  1. int main()  
  2. {  
  3.     string word;  
  4.     unsigned count = 0;  
  5.     vector strVec;  
  6.   
  7.     while (cin >> word)  
  8.     {  
  9.         ++ count;  
  10.         strVec.push_back(word);  
  11.     }  
  12.   
  13.     if (count == strVec.size())  
  14.     {  
  15.         cout << "Equal!" << endl;  
  16.     }  
  17.     //C++程序员应习惯于用 != 来限定循环的约束条件  
  18.     for (vector::size_type index = 0; index != strVec.size(); ++index)  
  19.     {  
  20.         cout << strVec[index] << endl;  
  21.     }  
  22.   
  23.     return 0;  
  24. }  

P82关键概念:安全的泛型编程 推荐阅读!】


6、下标操作不添加元素!

  1. vector<int> ivec;  
  2. for (vector<int>::size_type index = 0; index != 10; ++index)  
  3. {  
  4.     /* 
  5.     *必须是已经存在的元素才能使用下标操作符进行索引 
  6.     *通过下标操作符进行赋值时,并不会添加任何元素 
  7.     *ivec[index] = index + 1; ERROR 
  8.     */  
  9.     ivec.push_back(index + 1);  
  10. }  
  11. for (vector<int>::size_type index = 0; index != ivec.size(); ++index)  
  12. {  
  13.     cout << ivec[index] << endl;  
  14. }  
  15. for (vector<int>::size_type index = 0; index != ivec.size(); ++index)  
  16. {  
  17.     //对于已经存在的元素  
  18.     ivec[index] = 333;  
  19. }  
  20. for (vector<int>::size_type index = 0; index != ivec.size(); ++index)  
  21. {  
  22.     cout << ivec[index] << endl;  
  23. }  

7、试图获取不存在的元素必然导致运行时错误,但是,不能确保执行过程中可以捕捉到这类错误!

程序运行时总会以某种有趣的方式失败@_@


  1. //P83 习题3.13 (1)  
  2. int main()  
  3. {  
  4.     freopen("input.txt","r",stdin);  
  5.     vector<int> ivec;  
  6.     int value;  
  7.     while (cin >> value)  
  8.     {  
  9.         ivec.push_back(value);  
  10.     }  
  11.   
  12.     for (vector<int>::size_type index = 0; index < ivec.size() - 1; index += 2)  
  13.     {  
  14.         cout << ivec[index] + ivec[index + 1] << endl;  
  15.     }  
  16.     if (ivec.size() % 2)  
  17.     {  
  18.         cout << "The last element " << ivec[ivec.size() - 1]  
  19.              << " is not been summed!" << endl;  
  20.     }  
  21.     return 0;  
  22. }  

  1. //(2)  
  2. int main()  
  3. {  
  4.     freopen("input.txt","r",stdin);  
  5.     vector<int> ivec;  
  6.     int value;  
  7.   
  8.     while (cin >> value)  
  9.     {  
  10.         ivec.push_back(value);  
  11.     }  
  12.     vector<int>::size_type length = ivec.size();  
  13.     cout << "Length is: " << length << endl;  
  14.   
  15.     for (vector<int>::size_type index = 0; index < ivec.size()/2; ++index)  
  16.     {  
  17.         cout << ivec[index] + ivec[ivec.size() - 1 - index] << endl;  
  18.     }  
  19.     if (ivec.size() % 2)  
  20.     {  
  21.         cout << "The center element " << ivec[ivec.size() / 2]  
  22.              << " is not been summed!" << endl;  
  23.     }  
  24.     return 0;  
  25. }  

  1. //习题3.14  
  2. int main()  
  3. {  
  4.     freopen("input.txt","r",stdin);  
  5.     string word;  
  6.     vector strVec;  
  7.   
  8.     while (cin >> word)  
  9.     {  
  10.         strVec.push_back(word);  
  11.     }  
  12.   
  13.     for (vector::size_type i = 0;i != strVec.size(); ++i)  
  14.     {  
  15.         for (string::size_type j = 0;j != strVec[i].size(); ++j)  
  16.         {  
  17.             strVec[i][j] = toupper(strVec[i][j]);  
  18.         }  
  19.     }  
  20.   
  21.     for (vector::size_type index = 0;index != strVec.size(); ++index)  
  22.     {  
  23.         cout << strVec[index] << ' ';  
  24.         if (!((index+1) % 8))  
  25.         {  
  26.             cout << endl;  
  27.         }  
  28.     }  
  29.     return 0;  
  30. }  


转载:http://blog.csdn.net/zjf280441589/article/details/22977129
阅读(1195) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~