Вопрос по c++, stl – Есть ли более эффективный способ установить std :: vector из потока?

6

В настоящее время я устанавливаю значениеstd::vector<char> изstd::ostringstream следующее:

void
foo(std::vector<char> &data, std::stringstream &stream) {
  data = std::vector<char>(stream.str().begin(), stream.str().end());
}

Мне интересно, есть ли более эффективный способ сделать это с помощью STL в C ++ или метод, который я здесь привожу, считается подходящим? Буду ли я лучше использоватьstd::stringstream вместо?

Error: User Rate Limit Exceeded WilliamKF
Error: User Rate Limit ExceededisError: User Rate Limit Exceeded.str()Error: User Rate Limit Exceeded Robᵩ

Ваш Ответ

3   ответа
0

if there is a more efficient way

Error: User Rate Limit ExceededreserveError: User Rate Limit ExceededdataError: User Rate Limit ExceededinsertError: User Rate Limit ExceededdataError: User Rate Limit ExceededvectorError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededstd::distanceError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
10

Error: User Rate Limit Exceededstr()Error: User Rate Limit ExceededvectorError: User Rate Limit Exceeded

void foo(std::vector<char> &data, std::stringstream &stream) {
    const std::string& str = stream.str();
    data.assign( str.begin(), str.end() );
}

Error: User Rate Limit Exceededstd::stringError: User Rate Limit Exceededstd::istreambuf_iteratorError: User Rate Limit Exceeded

void foo(std::vector<char> &data, std::stringstream &stream) {
    data.assign(
        std::istreambuf_iterator<char>( stream ), std::istreambuf_iterator<char>()
    );
}

Error: User Rate Limit Exceededinput iteratorsError: User Rate Limit ExceededvectorError: User Rate Limit ExceededreserveError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededistreambuf_iteratorError: User Rate Limit Exceededrdbuf. :)
Error: User Rate Limit Exceededistreambuf_iteratorError: User Rate Limit Exceededistream_iteratorError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededcharError: User Rate Limit Exceeded
9

Error: User Rate Limit Exceededundefined behaviour. stream.str()Error: User Rate Limit Exceededby-valueError: User Rate Limit ExceededbeginError: User Rate Limit ExceededendError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>

int main(){
  std::stringstream src("....");
  std::vector<char> dest;
  // for a bit of efficiency
  std::streampos beg = src.tellg();
  src.seekg(0, std::ios_base::end);
  std::streampos end = src.tellg();
  src.seekg(0, std::ios_base::beg);
  dest.reserve(end - beg);

  dest.assign(std::istreambuf_iterator<char>(src), std::istreambuf_iterator<char>());

  std::copy(dest.begin(), dest.end(), std::ostream_iterator<char>(std::cout));
}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededstd::stringError: User Rate Limit Exceeded

std::string const& s = stream.str();
data.reserve(s.size());
data.assign(s.begin(), s.end());
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededsrc.rdbuf()->in_avail()Error: User Rate Limit ExceededseekError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded.assignError: User Rate Limit Exceededstd::coutError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded WilliamKF

Похожие вопросы