當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。