简体   繁体   English

在C ++中返回向量数组

[英]returning an array of vectors in C++

I'm having difficulty returning an array of string vectors. 我在返回字符串向量数组时遇到困难。 I have a function: 我有一个功能:

std::vector<std::string>* generatecVec(std::vector<std::string> strVec){
  std::vector<std::string> cVec[3];
  cVec[0].push_back("Test11");
  cVec[0].push_back("Test12");
  cVec[0].push_back("Test13");
  cVec[1].push_back("Test21");
  cVec[1].push_back("Test22");
  cVec[1].push_back("Test23");
  cVec[2].push_back("Test31");
  cVec[2].push_back("Test32");
  cVec[2].push_back("Test33");
  return cVec;
}

and later I use the function like 然后我使用像

std::vector<std::string> *cVec = generatecVec(strVec);
for(std::vector<string>::iterator it = cVec[0].begin(); it != cVec[0].end(); ++it) {
    std::cout << *it;
}

but I keep getting segmentation fault. 但我不断遇到细分错误。 I realize I must be using pointers improperly, but how might I fix this? 我意识到我一定使用了不正确的指针,但是我该如何解决呢? I use the array of vectors because it is very easy to refer to it by index (I only need three, non dynamic). 我使用向量数组,因为按索引引用它很容易(我只需要三个,非动态的)。 Thanks! 谢谢!

You are returning a pointer to something that only lives in the scope of the function. 您将返回一个指向仅位于函数范围内的对象的指针。 Once the function is done, cVec vanishes and the caller is left with a dangling pointer. 功能完成后, cVec消失,调用方留下一个悬空的指针。 I suggest returning an object that can actually be copied, such as an std::array<std::vector<std::string> 3> . 我建议返回一个实际上可以复制的对象,例如std::array<std::vector<std::string> 3>

#include <array> // for std::array

std::array<std::vector<std::string>,3> generatecVec(/*std::vector<std::string> strVec*/){
  std::array<std::vector<std::string>,3> cVec;
  cVec[0].push_back("Test11");
  cVec[0].push_back("Test12");
  cVec[0].push_back("Test13");
  cVec[1].push_back("Test21");
  cVec[1].push_back("Test22");
  cVec[1].push_back("Test23");
  cVec[2].push_back("Test31");
  cVec[2].push_back("Test32");
  cVec[2].push_back("Test33");
  return cVec;
}

I have commented out strvec here because it seems to play no role in the function. 我在这里注释了strvec ,因为它似乎在功能中不起作用。

You can then use it like this (C++11 range based for loop syntax): 然后,您可以像这样使用它(基于C ++ 11范围的循环语法):

auto cVec = generatecVec(); // no strVec because it doesn't play any role
for(auto it = cVec[0].cbegin(); it != cVec[0].cend(); ++it) {
    std::cout << *it;
}

Note that the push_backs may not be necessary if your compiler supports C++11 initializer list initialization. 请注意,如果您的编译器支持C ++ 11初始化程序列表初始化,则push_backs可能不是必需的。

If your compiler doesn't support std::array , try std::tr1::array from , or boost::array . 如果您的编译器不支持std::array ,请尝试从std::tr1::arrayboost::array

You're returning a pointer to an automatic array, which is destroyed as it goes out of scope and your pointer points to a destroyed array full of destroyed vectors. 您将返回一个指向自动数组的指针,该数组在超出范围时会被破坏,并且指针指向充满了被破坏矢量的被破坏数组。

Use std::array and return it by value: 使用std::array并按值返回:

// This means: std::array of 3 std::vector<string>
//   Type--VVVVVVVVVVVVVVVVVVVVVVVV  V-- Array size
std::array<std::vector<std::string>, 3> generatecVec(std::vector<std::string> strVec){
  return { {
     { "Test11", "Test12", "Test13" },
     { "Test21", "Test22", "Test23" },
     { "Test31", "Test32", "Test33" }
  } };
}

auto cVec = generatecVec(strVec);
for(auto it = cVec[0].begin(); it != cVec[0].end(); ++it) {
    std::cout << *it;
}

It is strange that you use vector of string when you need collection of strings, but pointer when you need collection of vectors. 奇怪的是,当需要收集字符串时使用字符串向量,而在需要收集向量时使用指针。 Using typedef should help with abstraction of details and see possible solution: 使用typedef应该有助于抽象细节并查看可能的解决方案:

typedef std::vector<std::string> strings;
typedef std::vector<strings> strings_seq;

strings_seq generateVec()
{
  strings_seq cVec( 3 );
  cVec[0].push_back("Test11");
  cVec[0].push_back("Test12");
  cVec[0].push_back("Test13");
  cVec[1].push_back("Test21");
  cVec[1].push_back("Test22");
  cVec[1].push_back("Test23");
  cVec[2].push_back("Test31");
  cVec[2].push_back("Test32");
  cVec[2].push_back("Test33");
  return cVec;
}

As the others already explained, you are returning a local reference, that is garbage outside the function scope. 正如其他人已经解释的那样,您正在返回一个本地引用,即函数范围之外的垃圾。 As a rule of thumb I try to avoid raw pointers as much as possible, since eventually I forget to delete a pointer after usage, or to initialize a pointer to start with. 根据经验,我会尽量避免使用原始指针,因为最终我会忘记在使用后删除指针,或者初始化一个以该指针开头的指针。

When you return a std::array or std::vector you invoke the copy constructor and receive a fresh copy of the vector,array,etc... I personally tend to use boost shared_ptr for these situations, as they overcome most disadvantages that come with the classic C pointers 当您返回std :: array或std :: vector时,您将调用副本构造函数并接收到vector,array等的新副本。我个人倾向于在这些情况下使用boost shared_ptr,因为它们克服了大多数缺点带有经典的C指针

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

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