<char> 通过转换将向量转换为字符串

Convert a vector<char> to a string with a conversion

本文关键字:转换 字符串 向量 lt char gt      更新时间:2023-10-16

我想将vector<char>转换为std::string并按照一种方式进行转换。

我快到了,但是下面的代码的结果是一个vector<string>,而我想有一个字符串(向量中所有字符串部分的串联)。

有关详细信息,请参阅我的代码示例。

string ConvertHexToAscii(const char input)
{
    std::ostringstream oss;
    oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(input);
    return oss.str();
}
vector<char> readBuffer; // this is my input
readBuffer.push_back(0x1c);
readBuffer.push_back(0x09);
vector<string> resultVec;
std::transform(readBuffer.begin(), readBuffer.end()
    , back_inserter(resultVec)
    , ConvertHexToAscii);
// resultVec[0] = "1C";
// resultVec[1] = "09";

我需要的结果是一个包含"1C09"的字符串。如何通过std::transform实现这一目标?

到了;这有效:

std::stringstream sstr;
std::transform(
    input.begin(), input.end(),
    std::ostream_iterator<std::string>(sstr, ""),
    ConvertHexToAscii);

但不幸的是,这会实例化相当多的字符串流,这是低效的。理想情况下,ConvertHexToAscii(顺便说一下,名称错误!C++不知道编码)函数将直接使用底层流。

#include <iostream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <numeric>
std::string ConvertHexToAscii(std::string acc, char input)
{
  std::ostringstream oss;
  oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(input);
  return acc + oss.str();
}

int main() {
  std::vector<char> readBuffer; // this is my input
  readBuffer.push_back(0x1c);
  readBuffer.push_back(0x09);
  std::cout << std::accumulate(readBuffer.begin(), readBuffer.end()
          , std::string(), ConvertHexToAscii) << std::endl;
  return 0;
}

back_insert_iterator为运算符 = 定义为

template< class string_type, class value_type >
class back_insert_iterator
{
public:
  back_insert_iterator< _string_type >& operator = ( const value_type& val )
  {
    container->append( val )
    return *this;
  }
};

您可以使用函数输出迭代器执行此操作:

#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <iomanip>
#include <boost/function_output_iterator.hpp>
std::string ConvertHexToAscii(const char input)
{
    std::ostringstream oss;
    oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(input);
    return oss.str();
}
int main() {
    std::vector<char> readBuffer; // this is my input
    readBuffer.push_back(0x1c);
    readBuffer.push_back(0x09);
    std::string temp;
    std::transform(readBuffer.begin(), readBuffer.end()
    , boost::make_function_output_iterator([&temp](const std::string& r) {temp.append(r);})
    , ConvertHexToAscii);

    std::cout << temp << std::endl;
}

我使用 lambda 在结果字符串上调用 append() 函数,但如果您没有可用的函数,那么使用boost::bind或者只是编写一个老式的函子来为您执行此操作是相当容易的

使用 boost::bind函数输出迭代器创建为:

boost::make_function_output_iterator(boost::bind(static_cast<std::string& (std::string::*)(const std::string&)>(&std::string::append), &temp, _1))

相反。它有点笨拙,因为您需要为std::string::append选择合适的重载。

虽然 perreal 使用累积的想法还不错,但直接在流上操作而不是创建这么多临时字符串可能会更有性能(尽管移动语义可能会有所帮助):

std::ostringstream os;
std::string temp = std::accumulate(
                       readBuffer.begin(), readBuffer.end(), std::ref(os), 
                       [](std::ostream &os, const char input) 
                       -> std::reference_wrapper<std::ostream> {
                           return os << std::hex << std::setw(2) 
                                     << std::setfill('0') 
                                     << static_cast<int>(input);
                       }).str();

编辑:但是好吧,这可能有点过于复杂,一个简单的foreach也可以(即使不像累积那样语义干净):

std::ostringstream os;
std::for_each(readBuffer.begin(), readBuffer.end(), 
              [&os](const char input) mutable {
                  os << std::hex << std::setw(2) << std::setfill('0') 
                     << static_cast<int>(input);
              });
std::string temp = os.str();

但是任何事情都可能比创建一大堆临时字符串更好。