简体   繁体   English

如何将项目放入const向量?

[英]How do I put items into a const vector?

I've been trying a bunch of different ways, but I can't figure it out. 我一直在尝试一些不同的方法,但我无法弄清楚。 The declaration that I was given is: 我得到的声明是:

const std::vector<std::string>&,

I've been trying to do this: 我一直试图这样做:

gradeReported.push_back(firstEntry);

I keep getting an error. 我一直在收到错误。 I am pretty sure it has something to do with the const nature of the vector. 我很确定它与向量的const性质有关。 Help would be appreciated! 帮助将不胜感激!

You don't. 你没有。 If it's const , it cannot be modified. 如果是const ,则无法修改。 You need to make a copy or take it by non-const reference. 您需要制作副本或通过非const引用来获取它。

You can't put items into a const vector, the vectors state is the items it holds, and adding items to the vector modifies that state. 您不能将项目放入const向量中,向量状态是它所包含的项目,向向量添加项目会修改该状态。

If you want to append to a vector you must take in a non const ref. 如果要附加到矢量,则必须使用非常量参考。

void f(std::vector<int>& v)
{ 
    v.push_back(4);
}

If you have a const vector it means you can only read the elements from that vector. 如果你有一个const向量,这意味着你只能从该向量中读取元素。
The same rules apply to a vector as to any other type when you use the const qualifier. 使用const限定符时,相同的规则适用于任何其他类型的向量。

To be able to modify it, You will either have to make a copy of the original vector or take a non const reference. 为了能够修改它,您将需要复制原始向量或采用非const引用。

The whole point of a const vector is that you cannot modify it. const向量的重点是你无法修改它。 Appending an element to it would be a modification and thus a violation of its constness. 向其添加元素将是一种修改,因此违反了其常量。 That's why you can't do it. 这就是为什么你不能这样做的原因。 It's by design. 这是设计的。

You're not supposed to be able to change it, that's kind of the point of the const. 你不应该能够改变它,这就是const的重点。 It's mostly used for passing in parameters to functions/methods and having an assurance that it won't be modified. 它主要用于将参数传递给函数/方法,并确保它不会被修改。

If you really need to change it, there are two options: 如果您确实需要更改它,有两种选择:

  1. Make a copy of the vector and modify that instead. 制作矢量的副本并修改它。
  2. Remove the const via a const_cast. 通过const_cast删除const。

Making a copy 制作副本

const std::vector<std::string>& foo;
std::vector<std::string> bar(foo);

Removing const via const_cast 通过const_cast删除const

I would strongly recommend against using this, but it is possibly an option. 我强烈建议不要使用它,但它可能是一个选项。

Assuming that the string vector is being given const protection before being passed as a parameter to a method or a function, and that the underlying type is not actually const , then the following can be done: 假设字符串向量在作为参数传递给方法或函数之前被赋予const保护,并且底层类型实际上不是const ,那么可以执行以下操作:

typedef std::vector<std::string> string_vector;
void f(const string_vector& foo) {
    const string_vector& foo;
    const_cast<string_vector>(foo).push(firstEntry);
}

int main() {
    string_vector foo();
    f(foo);
}

你必须在你的声明中删除const,或者如果不可能使用它

const_cast<std::vector<std::string>& >(gradeReported).push_back(firstEntry);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM