简体   繁体   English

C++ 向量用值初始化

[英]C++ vector initialize with value

As a C++ beginner, I know that we can initialize a vector of vectors using:作为一个 C++ 初学者,我知道我们可以使用以下方法初始化一个向量:

vector<vector<int>> vec(10, vector<int>(15, 0));

which is quite efficient, but when I want to do the same thing with a struct I wrote, The code won't compile:这是非常有效的,但是当我想对我编写的结构做同样的事情时,代码将无法编译:

vector<TreeNode> vec(10, TreeNode("abc"));

the struct looks like this:结构如下所示:

struct TreeNode {
    std::string val;
    TreeNode* left;
    TreeNode* right;
    explicit TreeNode(std::string& val) {
        this->val = val;
        left = nullptr, right = nullptr;
    }

};

I am wondering what's the problem here我想知道这里有什么问题

I used Clion as my IDE, IDE was reporting this error: No matching conversion for functional-style cast from 'const char [4]' to 'TreeNode'我使用 Clion 作为我的 IDE,IDE 报告此错误: No matching conversion for functional-style cast from 'const char [4]' to 'TreeNode'

And compiler was reporting this error:编译器报告了这个错误: 在此处输入图像描述

A string literal like "abc" isn't a std::string ."abc"这样的字符串文字不是std::string But that's okay!不过没关系! A std::string , such as the one that's a parameter in your TreeNode constructor, can be constructed from one automatically. std::string ,例如TreeNode构造函数中的参数,可以从一个自动构造。

However, that string will be a temporary , and temporaries don't bind to non- const references (except with a Visual Studio extension).但是,该字符串将是一个临时的,并且临时不绑定到非const引用(Visual Studio 扩展除外)。

That's okay too: you don't need them to;没关系:你不需要他们; your constructor can take a const std::string& instead and everything will be fine.您的构造函数可以采用const std::string&代替,一切都会好起来的。

Other solutions include taking a std::string by value, which is actually a good idea as then you can std::move from it inside the constructor, and now your constructor is efficient whether you're passing in a temporary or moving in some other named string.其他解决方案包括按值获取std::string ,这实际上是一个好主意,因为您可以在构造函数中从它std::move ,现在您的构造函数是有效的,无论您是传递一个临时的还是移动一些其他命名字符串。 But that's a story for another day.但这是另一天的故事。

As an aside, if this project is for production, and you are likely to have a lot more vectors than shown in your example, I strongly advise you don't nest them like this.顺便说一句,如果这个项目是用于生产的,并且您可能拥有比示例中显示的更多的向量,我强烈建议您不要像这样嵌套它们。 Each element has its own memory management and indirection, which is very wasteful.每个元素都有自己的memory管理和间接,非常浪费。 Your data are square (the inner dimension always has the same size), so all you actually need is a single vector of 10*15 int s.您的数据是正方形的(内部维度始终具有相同的大小),因此您实际需要的只是一个 10*15 int的向量。 Then those int s will all live in one nice big array, which is cache-friendly and only requires one allocation and one deallocation, Instead of, like 165. of each.然后这些int将全部存在于一个不错的大数组中,该数组对缓存友好,只需要一次分配和一次释放,而不是像每个 165. 一样。 Now you'll have to wrap your accesses in some maths (like index = y*width + x ) but that's trivial.现在你必须用一些数学方法来包装你的访问(比如index = y*width + x ),但这很简单。

The problem is not in the initialization of the std::vector (which is correct), but in the initialization of the TreeNode structure.问题不在于 std::vector 的初始化(这是正确的),而在于 TreeNode 结构的初始化。

This altered code works:此更改后的代码有效:

std::string initVal = "abc";
std::vector<TreeNode> vec(10, TreeNode(initVal));

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

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