Converting Std::String to Std::Vector<Char>

Converting std::string to std::vectorchar

Nope, that's the way to do it, directly initializing the vector with the data from the string.

As @ildjarn points out in his comment, if for whatever reason your data buffer needs to be null-terminated, you need to explicitly add it with charvect.push_back('\0').

Also note, if you want to reuse the buffer, use the assign member function which takes iterators.

How to convert a vectorstring to a vectorchar*

A faster way of doing this is

std::vector<const char*> charVec(strVec.size(),nullptr);
for (int i=0; i<strVec.size();i++) {
charVec[i]= strVec[i].c_str();
}

and then using the resulting vector. This will save a lot of time on memory allocation for large sets of data.

How to convert std::string to std::vectoruint8_t?

std::vector has a constructor just for this purpose:

std::string str;
std::vector<uint8_t> vec(str.begin(), str.end());

How to convert std::vectorstd::string to const char* array?

You can't convert it, but it's straightforward to create an array:

std::vector<const char*> strings;
for (int i = 0; i < list.size(); ++i)
strings.push_back(list[i].c_str();

And now, strings.data() gives you an array of const char*.

Note that strings should not be used after list has been destroyed, since it holds pointers to data that lives in list. I'd probably wrap this in a function:

void call_C_function(const std::vector<std::string>& list) {
std::vector<const char*> strings;
for (int i = 0; i < list.size(); ++i)
strings.push_back(list[i].c_str());
c_function(strings.data());
}

That way, strings will live only through the call to c_function, and there is no danger of it outlasting list.

How to copy std::string into std::vectorchar?

std::vector has a constructor that takes two iterators. You can use that:

std::string str = "hello";
std::vector<char> data(str.begin(), str.end());

If you already have a vector and want to add the characters at the end, you need a back inserter:

std::string str = "hello";
std::vector<char> data = /* ... */;
std::copy(str.begin(), str.end(), std::back_inserter(data));

How to construct a std::string from a std::vectorchar?

Well, the best way is to use the following constructor:

template<class InputIterator> string (InputIterator begin, InputIterator end);

which would lead to something like:

std::vector<char> v;
std::string str(v.begin(), v.end());

How to convert std::string to std::vectorstd::byte in C++17?

Using std::transform should work:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <vector>

int main()
{
std::string gpsValue;
gpsValue = "time[.........";
std::vector<std::byte> gpsValueArray(gpsValue.size() + 1);
std::transform(gpsValue.begin(), gpsValue.end(), gpsValueArray.begin(),
[] (char c) { return std::byte(c); });
for (std::byte b : gpsValueArray)
{
std::cout << int(b) << std::endl;
}
return 0;
}

Output:

116
105
109
101
91
46
46
46
46
46
46
46
46
46
0

std::vectorchar to std::string

Writing a null character at the end of a character vector will not magically create an std::string. To create an actual std::string, use something like:

std::string s = std::string(readBuffer.begin(), readBuffer.end());

You don't need to explicitly append the null character to the string, the std::string constructor will do it for you.

A more elegant way to copy std::string into a vectorchar

Construct a range with an iterator like this:

std::vector<char> temp(s.begin(), s.end());


Related Topics



Leave a reply



Submit