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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:29:58+00:00 2026-05-20T23:29:58+00:00

So I have the following string of data, which is being received through a

  • 0

So I have the following string of data, which is being received through a TCP winsock connection, and would like to do an advanced tokenization, into a vector of structs, where each struct represents one record.

std::string buf = "44:william:adama:commander:stuff\n33:luara:roslin:president:data\n"

struct table_t
{
    std::string key;
    std::string first;
    std::string last;
    std::string rank;
    std::additional;
};

Each record in the string is delimited by a carriage return. My attempt at splitting up the records, but not yet splitting up the fields:

    void tokenize(std::string& str, std::vector< string >records)
{
    // Skip delimiters at beginning.
    std::string::size_type lastPos = str.find_first_not_of("\n", 0);
    // Find first "non-delimiter".
    std::string::size_type pos     = str.find_first_of("\n", lastPos);
    while (std::string::npos != pos || std::string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        records.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of("\n", pos);
        // Find next "non-delimiter"
        pos = str.find_first_of("\n", lastPos);
    }
}

It seems totally unnecessary to repeat all of that code again to further tokenize each record via the colon (internal field separator) into the struct and push each struct into a vector. I’m sure there is a better way of doing this, or perhaps the design is in itself wrong.

Thank you for any help.

  • 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-20T23:29:59+00:00Added an answer on May 20, 2026 at 11:29 pm

    For breaking the string up into records, I’d use istringstream, if only
    because that will simplify the changes later when I want to read from
    a file. For tokenizing, the most obvious solution is boost::regex, so:

    std::vector<table_t> parse( std::istream& input )
    {
        std::vector<table_t> retval;
        std::string line;
        while ( std::getline( input, line ) ) {
            static boost::regex const pattern(
                "\([^:]*\):\([^:]*\):\([^:]*\):\([^:]*\):\([^:]*\)" );
            boost::smatch matched;
            if ( !regex_match( line, matched, pattern ) ) {
                //  Error handling...
            } else {
                retval.push_back(
                    table_t( matched[1], matched[2], matched[3],
                             matched[4], matched[5] ) );
            }
        }
        return retval;
    }
    

    (I’ve assumed the logical constructor for table_t. Also: there’s a very
    long tradition in C that names ending in _t are typedef’s, so you’re
    probably better off finding some other convention.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following string and I would like to remove <bpt *>*</bpt> and
I have the following serialization method: Private Function SerializeData(ByVal data As cData) As String
I have the following string (japanese) ユーザー名 , the first character is like whitespace
I have following string String str = replace :) :) with some other string;
Suppose I have following string: String asd = this is test ass this is
I have the following string: cn=abcd,cn=groups,dc=domain,dc=com Can a regular expression be used here to
I have the following string expression in a PowerShell script: select count(*) cnt from
I have the following string: <SEM>electric</SEM> cu <SEM>hello</SEM> rent <SEM>is<I>love</I>, <PARTITION />mind I want
I have the following string in the smarty (php templating system) variable $test: <img
Suppose you have the following string: white sand, tall waves, warm sun It's easy

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.