Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:57:10+00:00 2026-06-04T16:57:10+00:00

Presently, I set the value of a std::vector<char> from an std::ostringstream as follows: void

  • 0

Presently, I set the value of a std::vector<char> from an std::ostringstream as follows:

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

I’m wondering if there is a more efficient way to do this with STL in C++ or whether the method I give here is considered appropriate? Would I be better off using std::stringstream instead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:57:12+00:00Added an answer on June 4, 2026 at 4:57 pm

    Your method invokes undefined behaviour. stream.str() returns a string by-value, aka a temporary string. You take the begin iterator of one temporary and the end iterator of the other, creating an invalid range.

    One method to convert a stream to a container is to use the common iterator interface:

    #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));
    }
    

    Live example on Ideone.

    Another method would be to cache the returned std::string object:

    std::string const& s = stream.str();
    data.reserve(s.size());
    data.assign(s.begin(), s.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have situation where a user can manipulate a large set of data (presented
I'm interested in returning an empty result set from SQL Server stored procedures in
Presently troubleshooting a problem where running this SQL query: UPDATE tblBenchmarkData SET OriginalValue =
When comparing two key-value dictionary sets in C#: set A and set B, what
I thought this was simple like in Access. User needs to set the value
I have this code below. How can I set a cell value to =
I'm trying to select a value from a textbox that display a list of
I have a PHP-generated page which is displaying some data about a set of
Servlet API provides a convenient way to set cookies: response.addCookie(new Cookie(name, value)) JavaDoc tells:
I know how to get the value from the query string if the parameter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.