向量::在C++ STL中,()和vector:SWAP-()

载体 与动态数组相同,它能够在插入或删除元素时自动调整自身大小,其存储由容器自动处理。

null
向量::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);Output: 3Input: myvector = 3, 4, 1, 7, 3         myvector.at(3);Output: 7

错误和异常

  1. 如果向量中不存在该位置,则抛出 超出范围 .
  2. 除此之外,它也有一个强有力的保证。

C++

// 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, 9Output: 1 3 5 7 9Explanation - 1, 3, 5, 7 and 9 are at position 0, 2, 4, 6 and 8 which are even

算法

  1. 运行一个循环,直到向量的大小。
  2. 检查该位置是否可以被2整除,如果可以,则在该位置打印元素。

C++

// 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 

向量::交换()

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

语法:

vectorname1.swap(vectorname2)Parameters:The name of the vector with whichthe 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. 除此之外,它还有一个基本的无例外抛出保证。

C++

// 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 

如果向量的大小不同:

C++

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


输出

The vec1 contains: 200 200 200 200 200The vec2 contains: 100 100 100

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享