C++ 字符数组向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2392308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:17:40  来源:igfitidea点击:

C++ vector of char array

c++arrayscollections

提问by aHunter

I am trying to write a program that has a vector of char arrays and am have some problems.

我正在尝试编写一个具有字符数组向量的程序,但遇到了一些问题。

char test [] = { 'a', 'b', 'c', 'd', 'e' };

vector<char[]> v;

v.push_back(test);

Sorry this has to be a char array because I need to be able to generate lists of chars as I am trying to get an output something like.

抱歉,这必须是一个字符数组,因为我需要能够生成字符列表,因为我正在尝试获得类似的输出。

a a a b a c a d a e b a b c

aa ab ac ad ae ba bc

Can anyone point me in the right direction?

任何人都可以指出我正确的方向吗?

Thanks

谢谢

回答by

You cannot store arrays in vectors (or in any other standard library container). The things that standard library containers store must be copyable and assignable, and arrays are neither of these.

您不能将数组存储在向量中(或任何其他标准库容器中)。标准库容器存储的东西必须是可复制和可分配的,而数组两者都不是。

If you really need to put an array in a vector (and you probably don't - using a vector of vectors or a vector of strings is more likely what you need), then you can wrap the array in a struct:

如果您确实需要将数组放入向量中(而您可能不需要 - 使用向量向量或字符串向量更有可能是您所需要的),那么您可以将数组包装在一个结构中:

struct S {
  char a[10];
};

and then create a vector of structs:

然后创建一个结构向量:

vector <S> v;
S s;
s.a[0] = 'x';
v.push_back( s );

回答by Tronic

You need

你需要

char test[] = "abcde";  // This will add a terminating 
std::vector<char> v(test, test + sizeof(test)/sizeof(*test));
character to the array std::vector<std::string> v; v.push_back(test);

Of if you meant to make a vector of character instead of a vector of strings,

如果您打算制作字符向量而不是字符串向量,

std::string k ="abcde";
std::vector<std::string> v;
v.push_back(k);

The expression sizeof(test)/sizeof(*test)is for calculating the number of elements in the array test.

该表达式sizeof(test)/sizeof(*test)用于计算数组 test 中的元素数。

回答by Prasoon Saurav

Use std::stringinstead of char-arrays

使用std::string代替字符数组

boost::array<char, 5> test = {'a', 'b', 'c', 'd', 'e'};
std::vector<boost::array<char, 5> > v;
v.push_back(test);

回答by missingfaktor

You can use boost::arrayto do that:

您可以使用boost::array来做到这一点:

char test[] = {'a', 'b', 'c', 'd', 'e'};
std::vector<std::vector<char> > v;
v.push_back(std::vector<char>(test, test + sizeof(test)/ sizeof(test[0])));

Edit:

编辑:

Or you can use a vector of vectors as shown below:

或者您可以使用向量的向量,如下所示:

vector<char> c = {'a', 'b', 'c'};
vector < vector<char> > t = {{'a','a'}, 'b','b'};

回答by MAOXU

You can directly define a char type vector as below.

您可以直接定义一个 char 类型的向量,如下所示。

// g++ prog.cc -Wall -std=c++11
#include <iostream>
#include <vector>

 using namespace std;

 template<size_t N>
    inline 
      constexpr /* compile time */
      array<char,N> string_literal_to_array ( char const (&charrar)[N] )
 {
    return std::to_array( charrar) ;
 }

 template<size_t N>
    inline 
      /* run time */
      vector<char> string_literal_to_vector ( char const (&charrar)[N] )
 {
    return { charrar, charrar + N };
 }


int main()
{
   constexpr auto arr = string_literal_to_array("Compile Time");
   auto cv = string_literal_to_vector ("Run Time") ;
   return 42;
}

回答by Chef Gladiator

FFWD to 2019. Although this code worketh in 2011 too.

FFWD 到 2019 年。尽管此代码在 2011 年也有效。

//  1 - A std::vector of char*, more preper way is to use a std::vector<std::vector<char>> or std::vector<std::string>
std::vector<char*> v(10, "hi!");    //  You cannot put standard library containers e.g. char[] into std::vector!
for (auto& i : v)
{
    //std::cout << i << std::endl;
    i = "New";
}
for (auto i : v)
{
    std::cout << i << std::endl;
}

Advice: try optimizing the use of std::string. For char buffering std::array<char,N>is the fastest, std::vector<char>is faster.

建议:尝试优化使用std::string. 对于字符缓冲std::array<char,N>是最快的,std::vector<char>也更快。

https://wandbox.org/permlink/wcasstoY56MWbHqd

https://wandbox.org/permlink/wcasstoY56MWbHqd

回答by Nicholas Humphrey

What I found out is that it's OK to put char* into a std::vector:

我发现可以将 char* 放入 std::vector 中:

##代码##