c++中的array数组和vector数组

我觉得实验一下会记得比较牢,话不多直接上代码。

下面是array数组,感觉用的不多。

//cpp 风格数组 array
#include <iostream>
#include <array>
#include <vector>

using namespace std;

int main()
{
    array<int , 6> myint = {1 , 2 , 34, 45 , 0 , -2};
    for(int i = 0 ; i < myint.size() ; i++)  //size 获取长度,vector也是这样获取长度的
        cout << myint[i] <<"  " << (void *)&myint[i] << endl;

    array<int  , 5> a1 = {1 , 2 ,3  ,4 ,5};
    array<int ,  5> a2 = {0 , -1 , -2 , -3 , -5};
    array <int , 5> a3 = {8, 9 , 10 , 11 , 22};
    array<array<int , 5> , 3> a = {a1 , a2 , a3};


    for(int i = 0 ; i < a.size() ; i++)
    {
        for(int j = 0 ; j < a[0].size() ; j++)
            cout << a[i][j] << " ";
        cout << endl;
    }
    cout <<endl;

    for(auto i : a)  //c++11语法
    {
        for(auto j : i)
            cout << j << " ";
        cout <<endl;
    }
    cout <<endl;
}

下面是vector数组,觉得挺强大的。

一些基本的操作函数也不过 push_back() 尾部插入, pop_back() 尾部删除, size() 获取大小, erase() 指定位置删除, clear() 清空, insert() 指定位置插入 , empty() 判断数组是否为空 为空返回true, front() 返回第一个元素的引用, back() 返回最后一个元素的引用, begin() 返回首元素的迭代器, end() 返回尾元素的迭代器。

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<double>db;  //数组的大小可变 堆上的数组不一定是连续的
    double m;
    for(int i = 0 ; i < 4 ; i++)
    {
        cin >> m;          //不能直接cin输入到db中  因为还没分配内存
        db.push_back(m); //插入数据  自动变长
        cout << db[i] << "   " << (void*)&db[i] <<endl;
    }
        cout << db[1] << "   " << (void*)&db[1] << endl << endl;

        cout << &db <<endl;  //db不是指针

    for(auto i : db)  //这些i 和下面的ia ib ....都在栈上
    {
        cout << i << "  " << (void *)&i << endl;  //&i只能取首地址
    }

    cout << endl << endl;

    auto ia = db.begin();  //开始
    auto ib = db.end(); //结束
    for( ; ia != ib ; ia++)
    {
        cout << *ia << "   ";
    }
    cout <<endl;

    auto iia = db.rbegin();  //从尾部
    auto iib = db.rend();
    for( ; iia != iib ; iia++)
    {
        cout << *iia << " - ";
    }
}
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int >a1 , a2;
    vector<int >a3;

    a1.push_back(2);

    a2.push_back(3);
    a2.push_back(4);
    a2.push_back(5);

    a3.push_back(10);
    a3.push_back(99);

    vector<vector<int>> a = {a1 , a2 , a3};
    for( auto i : a)
    {
        for(auto j : i)
            cout << j << "   ";
        cout <<endl;
    }

    //多个vector可以实现嵌套  实现锯齿多维数组  长度可以不确定
    //多个array嵌套可以实现多维数组 但是长度必须等长

}
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector <string> str;

    str.push_back("WEL come!");
    str.push_back("Hello");
    str.push_back("World");
    str.push_back("China");
    str.pop_back(); //尾部删除一个
    //str.clear(); //清空

    for(auto ia = str.begin() ; ia != str.end() ; ia++)
    {
        if((*ia) == "Hello")
        {
            str.erase(ia);          //删除之后begin会发生变化
         //   break;
        }
        cout << *ia << endl;
    }

    str.erase(str.begin() + 1); // 删除

    cout << endl;

    for(auto i : str)  // 两种遍历输出  下面是另一种
    {
        cout << i << "    ";
    }
    cout << endl;
    str.insert(str.begin() +1 , "HHHH"); //不能越界插入  可以在范围内插入 

     for(auto i : str)  // 两种遍历输出  下面是另一种
    {
        cout << i << "    ";
    }
}
原文地址:https://www.cnblogs.com/Recoder/p/4757503.html