C++ 访问向量 [英] C++ accessing vector

查看:33
本文介绍了C++ 访问向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 std::vector 包含我自己的类,我必须访问它的函数和空值.

I have std::vector which contains my own class and I have to access its functions and voids.

class A
{
private: 
     int var;
     vector<string> vec;

public:
     void setVar(int i) { var = i; }
     void setVec(vector<string> a) { vec = a; }
};

我也有返回函数,但我懒得输入它们.我已经包含了所有必要的文件.

I have also return functions but I didn't bother to type them. And I have included all necessary files.

int main()
{
    vector<A> vec;
    for (int i = 0; i < 10; i++)
    {
        A tmp;
        tmp.setVar(i);
        vec.push_back(tmp);
    }

    for (int i = 0; i < 10; i++)
    {
        vector<string> tmp;
        tmp.push_back("1");
        tmp.push_back("2");
        tmp.push_back("3");
        vec.at(i).setVec(tmp);  //Works sometimes or causes error std::out_of_range
        vec[i].setVec(tmp);     //Crashes the whole programm
    }
}

既然 vector 已初始化,我该如何设置这些变量?

So how do I set those variables since the vector is initialized?

我使用的是 g++,这不是实际代码,因为我的实际代码很乱.

I am using g++ and this is not actual code because my actual code is messy.

Error for vec.at(i).setVec(tmp);
Error is:  terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check

推荐答案

您发布的代码仍然无法编译(在第一个循环中没有声明 tmp),所以让我大致解释一下发生.

The code you posted still won't compile (no tmp declared in the first loop), so let me explain in general what happens.

您提到的错误有两种:

vec.at(i).setVec(tmp);  //Works sometimes or causes error std::out_of_range

函数 at 试图确保安全 - 它首先检查向量的长度,然后返回给定索引的元素,或者抛出 std::out_of_range 如果该向量不包含此类索引的元素.

The function at tries to be safe - it first checks the length of the vector, then returns the element of the given index, or throws std::out_of_range if the vector doesn't contain the element of such index.

第二种情况:

vec[i].setVec(tmp);     //Crashes the whole programm

Operator [] 的行为很像向量的 at() 函数,但它不安全",因为它不进行任何边界检查.因此,如果您尝试访问 3 个元素的第 4 个元素,您只会访问内存中的某个随机位置(可能是另一个不相关的变量,例如,可能是其他任何东西).如果你很幸运,你的程序会在那之后崩溃.如果你不走运,你会遇到内存损坏问题和非常奇怪的错误,很难找到.

Operator [] behaves much like the at() function for vectors, but it not "safe" as it does not do any bounds-checking. Therefore, if you try to access 4-th element of a 3-element, you just access some random place in your memory (could be another, unrelated variable, for example, could be anything else). If you're lucky, your program will crash after that. If you're unlucky, you'll have memory corruption issues and very strange bugs, hard to find.

您的问题的解决方案是:

a) 用 vec.at(i) 替换 vec[i] - 工作相同(嗯,稍微慢一点,但你不会感觉到)而且你很安全.

a) Replace vec[i] with vec.at(i) - work the same (well, a tiny bit slower, but you won't feel that) and you're secure.

b) 然后:当您实际执行此向量查找时查看所有位置,并在每个位置停下一秒钟并考虑:此时此向量有多大?我确定此索引的元素存在吗?".

b) Then: Look at all the places when you actually do this vector lookup and in every place stop for a second and consider: "how big is this vector at this moment? am I sure that the element of this index exists?".

您很可能会通过这种方式快速找到您的错误.

You're likely to find your bug quickly this way.

这篇关于C++ 访问向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆