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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:20:45+00:00 2026-06-12T09:20:45+00:00

I’m trying to write a function that takes as an input argument a string,

  • 0

I’m trying to write a function that takes as an input argument a string, three output arguments that can be of varying types (at least that’s the idea) and a char that is a delimiter.

The input string is delimited by the specified char and the function assigns each char-terminated field to each of the output arguments in order (at the moment it takes care of input strings such as "a;bb;ccc" and is limited to three output arguments only but that’s not the problem).

For example with an input such as 10;200;3000 I’d get 10, 200 and 3000 for the first, second and third output arguments respectively

The output arguments need to be of either string or integer types, but due to my limited knowledge of C++ (and in particular generic programming) I’m having trouble writing a generic function that doesn’t care about that.

I have the following code:

template <typename T>
void parse_string (std::string &input, T &out1, T &out2, T &out3, const char delimiter)
{
    while (out3 == "")
    {
        std::string temp = input.substr(0, input.find(delimiter));
        input = input.substr(input.find(delimiter) +1);

        if (out1 == "") { out1 = temp;}
        else if (out2 == "") { out2 = temp;}
        else { out3 = temp;}
    }
}

and it works fine for strings but obviously not for integers.

I suspect I’m going wrong in the bits where I check if the argument is empty (among other parts I don’t know about).

Could you please help me improve it?

Also, I would welcome any ideas on improving the logic itself (perhaps I’d need to go with the variadic templates to make the number of arguments flexible, but
I’d have to check with our technical lead if C++11 standards are okay).

Ideally I’d like to avoid the situation where I have the exact same function twice but with a different signature for each of the types (one for strings and
one for ints).

Many thanks in advance!

  • 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-12T09:20:46+00:00Added an answer on June 12, 2026 at 9:20 am

    I would go with splitting the string into strings (as you do) and then converting them to required type.

    You can use boost::lexical_cast for that, or std::stringstream

    template <typename To, typename From>
    To cast(const From &arg)
    {
        std::stringstream s;
        s << arg;
        To res;
        s >> res;
    
        if (s.fail())
        {
            //throw some_exception();
        }
    
        return res;
    }
    

    In any case if you have boost available it is the right way to go

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.