📜  C++ STL中的vector :: at()和vector :: swap()

📅  最后修改于: 2021-05-30 05:43:11             🧑  作者: Mango

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

向量:: at()

at()函数用于引用作为函数参数给出的位置处的元素。
句法:

vectorname.at(position)
Parameters: 
Position of the element to be fetched.
Returns: 
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
// CPP program to illustrate
// Implementation of at() function
#include 
#include 
using namespace std;
 
int main()
{
    vector 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;
}


CPP
// CPP program to illustrate
// Application of at() function
#include 
#include 
using namespace std;
 
int main()
{
    vector 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;
}


CPP
// CPP program to illustrate
// Implementation of swap() function
#include 
#include 
using namespace std;
 
int main()
{
    // vector container declaration
    vector myvector1{ 1, 2, 3, 4 };
    vector 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;
}


C++
// CPP program
#include 
#include 
using namespace std;
 
int main()
{
    vector vec1{ 100, 100, 100 };
    vector vec2{ 200, 200, 200, 200, 200 };
 
    vec1.swap(vec2);
 
    cout << "The vec1 contains:";
    for (int i = 0; i < vec1.size(); i++)
        cout << ' ' << vec1[i];
    cout << '\n';
 
    cout << "The vec2 contains:";
    for (int i = 0; i < vec2.size(); i++)
        cout << ' ' << vec2[i];
    cout << '\n';
 
    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

// CPP program to illustrate
// Application of at() function
#include 
#include 
using namespace std;
 
int main()
{
    vector 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 
矢量:: swap()

此函数用于将一个向量的内容与相同类型的另一个向量交换,向量的大小可能不同。
句法:

vectorname1.swap(vectorname2)
Parameters:
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

// CPP program to illustrate
// Implementation of swap() function
#include 
#include 
using namespace std;
 
int main()
{
    // vector container declaration
    vector myvector1{ 1, 2, 3, 4 };
    vector 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 

如果向量的大小不同:

C++

// CPP program
#include 
#include 
using namespace std;
 
int main()
{
    vector vec1{ 100, 100, 100 };
    vector vec2{ 200, 200, 200, 200, 200 };
 
    vec1.swap(vec2);
 
    cout << "The vec1 contains:";
    for (int i = 0; i < vec1.size(); i++)
        cout << ' ' << vec1[i];
    cout << '\n';
 
    cout << "The vec2 contains:";
    for (int i = 0; i < vec2.size(); i++)
        cout << ' ' << vec2[i];
    cout << '\n';
 
    return 0;
}
输出
The vec1 contains: 200 200 200 200 200
The vec2 contains: 100 100 100

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”