编程语言
首页 > 编程语言> > C++中的几种数组:array,vector,valarray

C++中的几种数组:array,vector,valarray

作者:互联网

1. 内置数组(C语言风格数组)

  数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量。c++内置的原生数组可以存储一个固定大小的相同类型元素的顺序集合,并且其中的特定元素可以通过索引访问。它由连续的内存位置组成,最低的地址对应第一个元素,最高的地址对应最后一个元素。声明格式如下:

type arrayName [ arraySize ];

定义和初始化 :

// 默认初始化
int arr[5]; // 创建包含5个int型整数的数组,未被初始化
int arr[5] = {}; //创建并初始化5个值为0的int型元素
 
// 列表初始化
int arr[5] = {1, 2, 3}; // 显式的初始化数组的前三个元素,剩下的元素默认初始化为0
int arr[5] = {1, 2, 3, 4, 5 };
int arr[] = {1, 2, 3, 4, 5 }; // 等同于上

特点:

2. array

  array是c++11中引入的一个数组模板 ,是一种固定大小的容器类型,在定义的时候需要指定元素的类型和个数。array是一种更容易使用,更加安全的数组类型,可以用来替代内置数组。与内置的数组相比, array类模版支持几乎所有内置数组包含的特性,并且融入了很多容器操作,使得array在使用和性能上都要强于内置数组。声明格式如下:

array<typeName, arraySize> arrayName;

定义和初始化 :

// 默认初始化
array<int, 5> arr //创建具有5个int型元素的array<>,但未被初始化
array<int, 5> arr = {}; //创建并初始化5个值为0的int型元素
 
// 列表初始化
array<int, 5> arr = {1, 2, 3 }; //显式的初始化数组的前三个元素,剩下的元素默认初始化为0
array<int, 5> arr = {1, 2, 3, 4, 5 };

特点:

操作:

array<int,6> ar = {1, 2, 3, 4, 5, 6}; 
 
// 使用at()打印数组 
for ( int i=0; i<6; i++) 
    cout << ar.at(i) << " ";  
 
// 使用get()打印数组 
cout << get<0>(ar) << " " << get<1>(ar) << " "; 
cout << get<2>(ar) << " " << get<3>(ar) << " "; 
cout << get<4>(ar) << " " << get<5>(ar) << " "; 
 
// 使用operator[]打印数组 
for ( int i=0; i<6; i++) 
    cout << ar[i] << " "; 
array<int,6> ar = {1, 2, 3, 4, 5, 6}; 
 
// 打印数组的第一个元素  
cout << ar.front() << endl; 
// 打印数组的最后一个元素
cout << ar.back() << endl; 
array<int,6> ar = {1, 2, 3, 4, 5, 6}; 
 
// 打印数组中元素的数量
cout << ar.size() << endl; 
// 打印数组所能存放的元素的最大数目
cout << ar.max_size() << endl; 
array<int,6> ar = {1, 2, 3, 4, 5, 6}; 
array<int,6> ar1 = {7, 8, 9, 10, 11, 12}; 
 
// 将ar1的值与ar交换
ar.swap(ar1); 
array<int,6> ar; 
array<int,0> ar1; 
 
// 检查ar1的长度是否为空 
ar1.empty()? cout << "Array empty":  cout << "Array not empty";  
 
// 用0填充 ar
ar.fill(0); 

3. vector

  vector 是一种序列容器,可以看作是可变长的动态数组,支持随机访问迭代器,所有 STL算法都能对 vector 进行操作。创建vector变量时会自动在内存中分配一块连续的内存空间来保存数据,初始内存空间大小可以预先指定,也可以由vector默认指定大小。当存储的数据超过分配的空间时,vector会重新分配一块内存,但是这样的分配很耗时,步骤如下:

  在vector中,数据插入到最后。 在末尾插入需要花费不同的时间,因为有时可能需要扩展空间。删除最后一个元素需要的时间固定,因为不会发生大小调整。在开始或中间插入和删除元素的时间是线性的。
  vector的声明格式如下:

vector<typename> arrayName;
vector<typename> arrayname[arraySize];

定义和初始化:

// 默认初始化
vector<int> arr; //创建一个包含int类型的空vector数组
vector<int> arr[5]; //创建一个包含5个int型元素的vector数组,未被初始化
 
vector<int> arr(5); //创建一个vector数组并缺省初始化5个元素为0
vector<int> arr(5, 0); //创建一个vector数组并初始化5个元素为0
 
// 列表初始化
vector<int> arr{1, 2, 3, 4, 5 };
vector<int> arr = {1, 2, 3, 4, 5 };

特点:

操作:

#include <iostream> 
#include <vector> 
  
using namespace std; 
  
int main() 
{ 
    vector<int> g1; 
  
    for (int i = 1; i <= 5; i++) 
        g1.push_back(i); 
  
    cout << "Size : " << g1.size(); 
    cout << "\nCapacity : " << g1.capacity(); 
    cout << "\nMax_Size : " << g1.max_size(); 
  
    // resizes the vector size to 4 
    g1.resize(4); 
  
    // prints the vector size after resize() 
    cout << "\nSize : " << g1.size(); 
  
    // checks if the vector is empty or not 
    if (g1.empty() == false) 
        cout << "\nVector is not empty"; 
    else
        cout << "\nVector is empty"; 
  
    // Shrinks the vector 
    g1.shrink_to_fit(); 
    cout << "\nVector elements are: "; 
    for (auto it = g1.begin(); it != g1.end(); it++) 
        cout << *it << " "; 
  
    return 0; 
} 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    vector<int> g1; 
  
    for (int i = 1; i <= 10; i++) 
        g1.push_back(i * 10); 
  
    cout << "\nReference operator [g] : g1[2] = " << g1[2]; 
    cout << "\nat : g1.at(4) = " << g1.at(4); 
    cout << "\nfront() : g1.front() = " << g1.front(); 
    cout << "\nback() : g1.back() = " << g1.back(); 
  
    // pointer to the first element 
    int* pos = g1.data(); 
  
    cout << "\nThe first element is " << *pos; 
    return 0; 
} 
#include <bits/stdc++.h> 
#include <vector> 
using namespace std; 
  
int main() 
{ 
    // Assign vector 
    vector<int> v; 
  
    // fill the array with 10 five times 
    v.assign(5, 10); 
  
    cout << "The vector elements are: "; 
    for (int i = 0; i < v.size(); i++) 
        cout << v[i] << " "; 
  
    // inserts 15 to the last position 
    v.push_back(15); 
    int n = v.size(); 
    cout << "\nThe last element is: " << v[n - 1]; 
  
    // removes last element 
    v.pop_back(); 
  
    // prints the vector 
    cout << "\nThe vector elements are: "; 
    for (int i = 0; i < v.size(); i++) 
        cout << v[i] << " "; 
  
    // inserts 5 at the beginning 
    v.insert(v.begin(), 5); 
  
    cout << "\nThe first element is: " << v[0]; 
  
    // removes the first element 
    v.erase(v.begin()); 
  
    cout << "\nThe first element is: " << v[0]; 
  
    // inserts at the beginning 
    v.emplace(v.begin(), 5); 
    cout << "\nThe first element is: " << v[0]; 
  
    // Inserts 20 at the end 
    v.emplace_back(20); 
    n = v.size(); 
    cout << "\nThe last element is: " << v[n - 1]; 
  
    // erases the vector 
    v.clear(); 
    cout << "\nVector size after erase(): " << v.size(); 
  
    // two vector to perform swap 
    vector<int> v1, v2; 
    v1.push_back(1); 
    v1.push_back(2); 
    v2.push_back(3); 
    v2.push_back(4); 
  
    cout << "\n\nVector 1: "; 
    for (int i = 0; i < v1.size(); i++) 
        cout << v1[i] << " "; 
  
    cout << "\nVector 2: "; 
    for (int i = 0; i < v2.size(); i++) 
        cout << v2[i] << " "; 
  
    // Swaps v1 and v2 
    v1.swap(v2); 
  
    cout << "\nAfter Swap \nVector 1: "; 
    for (int i = 0; i < v1.size(); i++) 
        cout << v1[i] << " "; 
  
    cout << "\nVector 2: "; 
    for (int i = 0; i < v2.size(); i++) 
        cout << v2[i] << " "; 
} 

参考https://blog.csdn.net/ye1223/article/details/79772771
   https://www.geeksforgeeks.org/vector-in-cpp-stl/

4. valarray

  valarray是一个类模板,面向数值计算,优势是重载了各种数学函数,方便数学计算。

定义与初始化

valarray<int> arr(5); //创建包含5个int型元素的valarray
valarray<int> arr(1, 5); //创建包含5个值为1的元素的int型valarray
 
int a[] = {1, 2, 3, 4, 5 };
valarray<int> arr(a, 5); //创建包含5个int型元素的valarray,初始值为a[]

特点

操作

#include<iostream> 
#include<valarray> // for valarray functions 
using namespace std; 
int main() 
{ 
    // Initializing valarray 
    valarray<int> varr = { 10, 2, 20, 1, 30 }; 
      
    // Declaring new valarray 
    valarray<int> varr1 ; 
      
    // Using apply() to increment all elements by 5 
    varr1 = varr.apply([](int x){return x=x+5;}); 
      
    // Displaying new elements value 
    cout << "The new valarray with manipulated values is : "; 
    for (int &x: varr1) 
        cout << x << " "; 
    cout << endl; 
      
    // Displaying sum of both old and new valarray 
    cout << "The sum of old valarray is : "; 
    cout << varr.sum() << endl; 
    cout << "The sum of new valarray is : "; 
    cout << varr1.sum() << endl; 
  
    return 0; 
} 
valarray<int> varr = { 10, 2, 20, 1, 30 }; 
    
cout << varr.max() << endl; 
cout << varr.min() << endl; 
#include<iostream> 
#include<valarray> // for valarray functions 
using namespace std; 
int main() 
{ 
    // Initializing valarray 
    valarray<int> varr = { 10, 2, 20, 1, 30 }; 
      
    // Declaring new valarray 
    valarray<int> varr1; 
      
    // using shift() to shift elements to left 
    // shifts valarray by 2 position 
    varr1 = varr.shift(2); 
      
    // Displaying elements of valarray after shifting 
    cout << "The new valarray after shifting is : "; 
    for ( int&x : varr1) cout << x << " "; 
    cout << endl; 
      
    // using cshift() to circulary shift elements to right 
    // rotates valarray by 3 position 
    varr1 = varr.cshift(-3); 
      
    // Displaying elements of valarray after circular shifting 
    cout << "The new valarray after circular shifting is : "; 
    for ( int&x : varr1) 
        cout << x << " "; 
    cout << endl; 
  
    return 0; 
} 
valarray<int> varr1 = {1, 2, 3, 4}; 
valarray<int> varr2 = {2, 4, 6, 8}; 
 
varr1.swap(varr2); 

代码来源:https://www.geeksforgeeks.org/the-c-standard-template-library-stl/

标签:int,元素,C++,vector,数组,array,valarray
来源: https://blog.csdn.net/Csdn_Darry/article/details/118566032