三维vector及vector使用

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

今天张宁问我一个三维vector的问题,这我哪里知道啊,于是百度了一下,于是有了下面的一个三维vector的代码,也算是了解了一下这个三维vector的知识。


提示:以下是本篇文章正文内容,下面案例可供参考

一、三维vector

#include <iostream>
// for 3D vector
#include <vector>
using namespace std;
int main()
{
// Initializing 3D vector "vect3d" with
// user provided values
//here creating the vector of 1D of 2 size, 2D of 2 size and 3D of 3 size
std::vector<std::vector<std::vector<int> > > vect3d(2, std::vector<std::vector<int> > (2, std::vector<int>(3)));
// Inserting elements into the vector
for (int i = 0; i < vect3d.size(); i++) {
cout << "Elements at block "
<< i << ": ";
// Inserting element at each column,
// 0 is the starting iterator,
// size() is the ending iterator
for (int j = 0; j != vect3d[i].size(); j++) {
cout << "Elements at row "
<< j << ": ";
for (int k = 0; k != vect3d[i][j].size(); k++) {
cout<<"\nEnter number: ";
// use all indexes to insert the values into the vector
cin >> vect3d[i][j][k];
}
cout << endl;
}
}
// Displaying the 3D vector by using 3 iterator
cout << "The vectore values are: " <<" ";
for (int i = 0; i < vect3d.size(); i++) {
// Displaying element at each column,
// 0 is the starting iterator,
// size() is the ending iterator
for (int j = 0; j != vect3d[i].size(); j++) {
for (int k = 0; k != vect3d[i][j].size(); k++) {
// use all indexes to get the values store in the vector
cout << vect3d[i][j][k]<< ' ';
}
cout << endl;
}
}
return 0;
}

自己本地跑了一下,可以执行。 

二、vector初步使用

下面验证的是vector使用的一些情况。包括验证map插入值之后的size。以及vector判断首尾相同,以及vec[vec.size()]的值是什么。

#include <iostream>
#include <string>
#include <map>
#include <vector>

int main()
{
    std::map<std::string, int> testMap;
    testMap["hello"] = 3;
    testMap["world"] = 4;
    testMap["world"]++;
    printf("testMap.size:%d\n", testMap.size());        //符合预期,值是2
    printf("testMap:%d\n", testMap["hello"]);           //符合预期,值是3
    printf("testMap:%d\n", testMap["world"]);           //符合预期,值是5

    std::vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(1);
    std::cout << "vec size: " << vec.size() << std::endl;
    printf("vec[vec.size()]:%d\n", vec[vec.size()]);    //有问题,打印vec[vec.size()]值不是预期的1
    if (vec[0] == vec[vec.size()]) {                    //有问题,vec[vec.size()]数组越界了
        printf("equal\n");
    }

    if (vec.begin() == vec.end()) {                     // 有问题,不能这么判断首尾是否相等
        printf("vec.begin() and vec.end() equal\n");
    }

    printf("vec.front():%d\n", vec.front());
    printf("vec.back():%d\n", vec.back());
    if (vec.front() == vec.back()) {
        printf("vec.front() and vec.back() equal\n");
    }


    return 0;
}

//打印结果
//testMap.size:2
//testMap:3
//testMap:5
//vec size: 4
//vec[vec.size()]:0
//vec.front():1
//vec.back():1
//vec.front() and vec.back() equal


总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值