当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ vector::at()、vector::swap()用法及代码示例


向量与动态数组相同,具有在插入或删除元素时自动调整自身大小的能力,并且容器自动处理其存储。

vector::at()

at()函数用于参考在作为函数参数给出的位置上存在的元素。句法:

vectorname.at(position)
参数: 
Position of the element to be fetched.
返回: 
Direct reference to the element at the given position.

例子:


Input: myvector = 1, 2, 3
         myvector.at(2);
Outpu:3

Input: myvector = 3, 4, 1, 7, 3
         myvector.at(3);
Output: 7

错误和异常

  1. 如果向量中不存在该位置,则抛出out_of_range。
  2. 否则,它具有强大的无异常抛出保证。
// CPP program to illustrate 
// Implementation of at() function 
#include <iostream> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    vector<int> myvector; 
    myvector.push_back(3); 
    myvector.push_back(4); 
    myvector.push_back(1); 
    myvector.push_back(7); 
    myvector.push_back(3); 
    cout << myvector.at(3); 
    return 0; 
}

输出:

7

应用范围:
给定一个整数向量,打印出现在偶数位置的所有整数。

Input: 1, 2, 3, 4, 5, 6, 7, 8, 9
Output: 1 3 5 7 9
Explanation - 1, 3, 5, 7 and 9 are at position 0, 2, 4, 6 and 8 which are even

算法

  1. 循环运行直到向量的大小。
  2. 检查该位置是否可被2整除,如果是,则在该位置打印元素。
// CPP program to illustrate 
// Application of at() function 
#include <iostream> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    vector<int> myvector; 
    myvector.push_back(1); 
    myvector.push_back(2); 
    myvector.push_back(3); 
    myvector.push_back(4); 
    myvector.push_back(5); 
    myvector.push_back(6); 
    myvector.push_back(7); 
    myvector.push_back(8); 
    myvector.push_back(9); 
    // vector becomes 1, 2, 3, 4, 5, 6, 7, 8, 9 
  
    for (int i = 0; i < myvector.size(); i += 2) { 
  
        cout << myvector.at(i); 
        cout << " "; 
    } 
  
    return 0; 
}

输出:

1 3 5 7 9
vector::swap()

此函数用于将一个向量的内容与相同类型和大小的另一个向量交换。

用法:

vectorname1.swap(vectorname2)
参数:
The name of the vector with which
the contents have to be swapped.
Result: 
All the elements of the 2 vectors are swapped.

例子:

Input:myvector1 = {1, 2, 3, 4}
         myvector2 = {3, 5, 7, 9}
         myvector1.swap(myvector2);
Output:myvector1 = {3, 5, 7, 9}
         myvector2 = {1, 2, 3, 4}

Input:myvector1 = {1, 3, 5, 7}
         myvector2 = {2, 4, 6, 8}
         myvector1.swap(myvector2);
Output:myvector1 = {2, 4, 6, 8}
         myvector2 = {1, 3, 5, 7}

错误和异常

  1. 如果向量不是同一类型,则会引发错误。
  2. 否则,它具有基本的无异常抛出保证。
// CPP program to illustrate 
// Implementation of swap() function 
#include <iostream> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    // vector container declaration 
    vector<int> myvector1{ 1, 2, 3, 4 }; 
    vector<int> myvector2{ 3, 5, 7, 9 }; 
  
    // using swap() function to swap 
    // elements of vector 
    myvector1.swap(myvector2); 
  
    // printing the first vector 
    cout << "myvector1 = "; 
    for (auto it = myvector1.begin(); 
         it < myvector1.end(); ++it) 
        cout << *it << " "; 
  
    // printing the second vector 
    cout << endl 
         << "myvector2 = "; 
    for (auto it = myvector2.begin(); 
         it < myvector2.end(); ++it) 
        cout << *it << " "; 
    return 0; 
}

输出:

myvector1 = 3 5 7 9 
myvector2 = 1 2 3 4 


相关用法


注:本文由纯净天空筛选整理自AyushSaxena大神的英文原创作品 vector::at() and vector::swap() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。