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 6319955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:53:20+00:00 2026-05-24T15:53:20+00:00

I need to implement a mechanism where I can initialize a vector of my

  • 0

I need to implement a mechanism where I can initialize a vector of my custom class using a text source, where each line of the source is representing one instance of my class. To achieve this, I implemented the operator >> for my class and stringstream. When I read the source, I go line-by-line and get a substream of my original source, then parse the substream each time. This has three benefits for me. First, this way I can make sure that one line of the text source would represent exactly one instance of my class. Second, as the rest of the line after parsing is ignored, I can safely add any comment in any line of my text source, which would surely get ignored by the parser. And third, I don’t need to mention the length of the vector in my original source, since the first time I get a parsing error (I check the fail and bad bits of the stream to confirm this) I know that the vector declaration is over.

To parse line-by-line, I’m using the following code:

std::stringstream       fullStream;
std::stringstream       lineStream;
std::string             str;
bool                    isValid;
myClass                 newInstance;
std::vector < myClass > result;

// Fill fullStream from external source (codepart omitted)
isValid = true;
while ( isValid && ! fullStream.eof ( ) ) {
   std::getline ( fullStream, str );
   lineStream.clear ( );
   lineStream.str ( str );
   lineStream >> newInstance;
   isValid = ! lineStream.fail ( );
   if ( isValid ) {
      result.push_back ( newInstance );
   }
}

Although this code works fine, I’m wondering if there was a better way to achieve the same result. Specially, if there was a more efficient way to extract a line from fullStream to lineStream.

Thanks,
Ádám

  • 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-05-24T15:53:21+00:00Added an answer on May 24, 2026 at 3:53 pm

    One obvious alternative would be to have your operator>> do line-by-line reading itself, so you don’t have to do that externally:

    class MyClass { 
        // some sort of data to demonstrate the idea:
        int x;
        std::string y;
    
        friend std::istream &operator>>(std::istream &is, MyClass &m) { 
            std::string temp;
            std::getline(is, temp);
            std::istringstream buffer(temp);
            buffer >> m.x >> m.y;
            return is;
        }
    };
    

    With that, code to read data from a file becomes a little more straightforward:

    std::copy(std::istream_iterator<MyClass>(fullStream),
              std::istream_iterator<MyClass>(),
              std::back_inserter(result));
    

    Edit: if you don’t want to build the line-oriented reading directly into the operator>> for MyClass, another possibility is to use a proxy class:

    class LineReader { 
        MyClass object;
    public:
        operator MyClass() { return object; }
    
        friend std::istream &operator>>(std::istream &is, LineReader &d) { 
            std::string line;
            std::getline(is, line);
            std::istringstream buffer(line);
            buffer >> d; // delegate to the object's own stream-oriented reader.
        }
    };
    

    Then when you want to do line-oriented reading, you read objects of the proxy class, but store objects of the original class:

    std::vector<MyClass>((std::istream_iterator<LineReader>(some_stream)), 
                          std::istream_iterator<LineReader>());
    

    But, when/if you want to read a stream of objects instead of lines of objects, you use the object’s own operator>> directly:

    std::vector<MyClass>((std::istream_iterator<MyClass>(stream),
                          std::istream_iterator<MyClass>());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement a notification mechanism for a system that has one manager
I need to implement some form of communication mechanism in my application, to send
I need ideas to implement a (really) high performance in-memory Database/Storage Mechanism in Java.
I need ideas to implement a (really) high performance in-memory Database/Storage Mechanism. In the
I need some suggestions for how to implement a very basic mechanism that logs
I need to customize the normal TCP implementation so that I can implement and
For various reasons, I need to implement a type caching mechanism in C#. Fortunately,
I need a mechanism to implement the following scenario: two or more threads need
I need to implement a heartbeat-mechanism that sends a 'touch'-message to an external service
I need to implement a Diff algorithm in VB.NET to find the changes between

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.