将c字符串有效地转换为std::vector

时间:2023-01-22 16:38:03

I want to convert a C-style string into a byte-vector. A working solution would be converting each character manually and pushing it on the vector. However, I'm not satisfied with this solution and want to find a more elegant way.

我想把c风格的字符串转换成字节向量。一个可行的解决方案是手工转换每个字符并将其推到向量上。然而,我对这个解决方案并不满意,我希望找到一种更优雅的方式。

One of my attempts was the following:

我的一个尝试是:

std::vector<byte> myVector;
&myVector[0] = (byte)"MyString";

which bugs and gets me an

哪个虫子给了我一个

error C2106: '=': left operand must be l-value

错误C2106: '=':左操作数必须为l值

What is the correct way to do this?

正确的做法是什么?

6 个解决方案

#1


8  

The most basic thing would be something like:

最基本的东西是:

const char *cstr = "bla"
std::vector<char> vec(cstr, cstr + strlen(cstr));

Of course, don't calculate the length if you know it.

当然,如果你知道的话,不要计算长度。

The more common solution is to use the std::string class:

更常见的解决方案是使用std::string类:

const char *cstr;
std::string str = cstr;

#2


5  

STL containers such as vector always take ownership, i.e. they manage their own memory. You cannot modify the memory managed internally by an STL container. For that reason, your attempt (and any similar attempt) is doomed to failure.

STL容器(例如vector)总是拥有所有权,也就是说,它们管理自己的内存。您不能修改STL容器内部管理的内存。因此,你的尝试(以及任何类似的尝试)注定要失败。

The only valid solution is to copy the memory or to write a new STL-style container that doesn’t take ownership of the memory it accesses.

唯一有效的解决方案是复制内存或编写一个新的stl样式的容器,该容器不会占用它访问的内存。

#3


0  

std::vector<byte> myVector;
const char* str = "MyString";
myVector.assign( str, str+strlen(str) );

#4


0  

The most obvious question would be why you don't just use std::string:

最明显的问题是你为什么不使用std::string:

std::string myString("MyString");

but if you really think you need a vector:

但是如果你真的认为你需要一个向量:

char myString[] = "MyString";

std::vector<byte> myVector(myString, myString+sizeof(myString));

You might also want to consider using std::tr1::array:

您可能还想考虑使用std:::tr1:::array:

std::tr1::array<byte, sizeof("MyString")> myArray = {"MyString"};

C++ 0x will also have std::array.

c++ 0x还有std:::array。

#5


0  

Something along these lines should work

沿着这些线的东西应该是有用的

 std::vector<byte> myVector = std::vector((byte*)cstring, (byte*)ctring + strlen(cstring))

Also, this is still going to just iterate through the c string and insert the values into the vector. Like Konrad said, that's just how you have to do it since vectors manage their own memory.

同样,它仍然会遍历c字符串并将值插入到向量中。就像Konrad说的,这就是你要做的因为向量管理它们自己的记忆。

#6


-2  

const char *cstr = "bla"
std::vector<char> vec;
vec.resize(strlen(cstr)+1);
strcpy(&vec[0],cstr);

#1


8  

The most basic thing would be something like:

最基本的东西是:

const char *cstr = "bla"
std::vector<char> vec(cstr, cstr + strlen(cstr));

Of course, don't calculate the length if you know it.

当然,如果你知道的话,不要计算长度。

The more common solution is to use the std::string class:

更常见的解决方案是使用std::string类:

const char *cstr;
std::string str = cstr;

#2


5  

STL containers such as vector always take ownership, i.e. they manage their own memory. You cannot modify the memory managed internally by an STL container. For that reason, your attempt (and any similar attempt) is doomed to failure.

STL容器(例如vector)总是拥有所有权,也就是说,它们管理自己的内存。您不能修改STL容器内部管理的内存。因此,你的尝试(以及任何类似的尝试)注定要失败。

The only valid solution is to copy the memory or to write a new STL-style container that doesn’t take ownership of the memory it accesses.

唯一有效的解决方案是复制内存或编写一个新的stl样式的容器,该容器不会占用它访问的内存。

#3


0  

std::vector<byte> myVector;
const char* str = "MyString";
myVector.assign( str, str+strlen(str) );

#4


0  

The most obvious question would be why you don't just use std::string:

最明显的问题是你为什么不使用std::string:

std::string myString("MyString");

but if you really think you need a vector:

但是如果你真的认为你需要一个向量:

char myString[] = "MyString";

std::vector<byte> myVector(myString, myString+sizeof(myString));

You might also want to consider using std::tr1::array:

您可能还想考虑使用std:::tr1:::array:

std::tr1::array<byte, sizeof("MyString")> myArray = {"MyString"};

C++ 0x will also have std::array.

c++ 0x还有std:::array。

#5


0  

Something along these lines should work

沿着这些线的东西应该是有用的

 std::vector<byte> myVector = std::vector((byte*)cstring, (byte*)ctring + strlen(cstring))

Also, this is still going to just iterate through the c string and insert the values into the vector. Like Konrad said, that's just how you have to do it since vectors manage their own memory.

同样,它仍然会遍历c字符串并将值插入到向量中。就像Konrad说的,这就是你要做的因为向量管理它们自己的记忆。

#6


-2  

const char *cstr = "bla"
std::vector<char> vec;
vec.resize(strlen(cstr)+1);
strcpy(&vec[0],cstr);