zoukankan      html  css  js  c++  java
  • C++ Vector用法深入剖析

    C++ Vector用法深入剖析

    C++编程语言中有一种叫做Vector的应用方法,它的作用在实际编程中是非常重要的。在这里我们将会为大家详细介绍一下C++ Vector的相关应用技巧及基本内容,希望能给大家带来一些帮助。

    (1)vector< 类型 > 标识符 ;

    (2)vector< 类型 > 标识符(最大容量) ;

    (3)vector< 类型 > 标识符(最大容量,初始所有值);

    (4) int i[4] = {12,3,4,5};

    1. vector< 类型 > vi(i , i+2); //得到i索引值为3以后的值 ;  

    (5)vector< vector<int> > //vi 定义2维的容器;记得一定要有空格,不然会报错

    1. vector< int > line   
    2. // 在使用的时候一定要首先将vi个行进行初始化;   
    3. for(int i = 0 ; i < 10 ; i ++)  
    4. {  
    5. vector.push_back(line);  
    6. }  
    7. /// 个人认为使用vector定义二维数组很好,
      因为是长度可以不预先确定。很好。 

    (6)C++ Vector排序

    1. vector< int > vi ;   
    2. vi.push_back(1);  
    3. vi.push_back(3);  
    4. vi.push_back(0);  
    5. sort(vi.begin() , vi.end()); /// /小到大  
    6. reverse(vi.begin(),vi.end()) /// 从大道小 

    (7)顺序访问

    1. vector < int > vi ;   
    2. for( int i = 0 ; i < 10 ; i ++)  
    3. {  
    4. vi.push_back(i);  
    5. }   
    6. for(int i = 0 ; i < 10 ; i ++) /// 第一种调用方法  
    7. {  
    8. cout <<vi[i] <<" " ;   
    9. }  
    10. for(vector<int>::iterator it = vi.begin() ; 
    11. it !=vi.end() ; it++) ///第二种调用方法  
    12. {  
    13. cout << *it << " " ;  

    (8)寻找

    1. vector < int > vi ;   
    2. for( int i = 0 ; i < 10 ; i ++)  
    3. {  
    4. vector.push_back(i);  
    5. }   
    6. vector < int >::interator it = find(vi.begin() , vi.end,3) ;  
    7. cout << *it << endl ; ///返回容器内找到值的位置。 

    (9)使用数组对C++ Vector进行初始化

    1. int i[10] ={1,2,3,4,5,6,7,78,8} ;  
    2. ///第一种   
    3. vector<int> vi(i+1,i+3); ///从第2个元素到第三个元素  
    4. for(vector <int>::interator it = vi.begin() ; 
    5. it != vi.end() ; it++)  
    6. {  
    7. cout << *it <<" " ;   

    (10) 结构体类型

    1. struct temp  
    2. {  
    3.   public :  
    4.   string str ;   
    5.   public :  
    6.   int id ;  
    7. }tmp;
    8.  
    9. int main()  
    10. {  
    11. vector <temp> t ;   
    12. temp w1 ;   
    13. w1.str = "Hellowor" ;  
    14. w1.id = 1 ;   
    15. t.push_back(t1);  
    16. cout << w1.str << "," <<w1.id <<endl ;   
    17. return 0 ;   

    C++ Vector的基本介绍就为大家介绍到这里。



    vector::push_back


    public member function
    void push_back ( const T& x );

    Add element at the end

    Adds a new element at the end of the vector, after its current last element. The content of this new element is initialized to a copy of x.

    This effectively increases the vector size by one, which causes a reallocation of the internal allocated storage if the vector size was equal to the vector capacity before the call. Reallocations invalidate all previously obtained iterators, references and pointers.

    Parameters

    x
    Value to be copied to the new element.
    T is the first template parameter (the type of the elements stored in the vector).



    Return value

    none

    If a reallocation happens, it is performed using Allocator::allocate(), which may throw exceptions (for the defaultallocator, bad_alloc is thrown if the allocation request does not succeed).

    Example

    // vector::push_back
    #include <iostream>
    #include <vector>
    
    int main ()
    {
      std::vector<int> myvector;
      int myint;
    
      std::cout << "Please enter some integers (enter 0 to end):
    ";
    
      do {
        std::cin >> myint;
        myvector.push_back (myint);
      } while (myint);
    
      std::cout << "myvector stores " << int(myvector.size()) << " numbers.
    ";
    
      return 0;
    }

    The example uses push_back to add a new element to the vector each time a new integer is read.

    Complexity

    Constant (amortized time, reallocation may happen).

    See also

    vector::pop_back Delete last element (public member function)
    vector::insert Insert elements (public member function)
  • 相关阅读:
    富可视M310刷机包 MIUIV5 红米开发版 闪光 美化 稳定
    Windowsclient SSH 远程连接Windowsserver(PowerShell Server)
    数据结构与算法02--链表基础
    rhadoop linear regression 问题
    奇怪的git代理超时问题
    怎样利用Heartbeat与Floating IP在Ubuntu 14.04上创建高可用性设置
    IVS_原理
    NN入门
    算法体系
    CNN原理
  • 原文地址:https://www.cnblogs.com/xiao-wei-wei/p/3353325.html
Copyright © 2011-2022 走看看