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

The Archive Base Latest Questions

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

Consider I have the following pair of floats object input format: (first variable, second

  • 0

Consider I have the following “pair of floats” object input format: (first variable, second variable) e.g. (1.0, 15.6).

What’s the best way reading such structures? In C I would use scanf(“(%f, %f)”, &var1, &var2) – pretty nice, isn’t it?(yes, I know it doesn’t provide type safety and so on)

But I know only one way to do that using C++ streams:

float var1, var2;
char tmp;
cin >> tmp;
cin >> var1;
cin >> tmp;
cin >> var2;
cin >> tmp;

Seems ugly, and it’s just a pair of floats. So, is there an elegant way to do that? Like

cin >> "(" >> var1 >> ", " >> var2 >> ")";
  • 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-24T10:54:00+00:00Added an answer on May 24, 2026 at 10:54 am

    I would write an input operator:
    Stealing FloatPair from @Seth Carnegie.

    Thus input now looks normal:

    FloatPair c;
    std::cin >> c;
    

    I would make it look like this.

    std::istream& operator>>(std::istream& stream, FloatPair& out)
    {
        return stream >> I('(') >> out.a >> I(',') >> out.b >> I(')');
    }
    

    Then I have an ignore object like this.
    It is simple to templatise if you wish. And to make the code slightly easier I call mine I

    template<typename T>
    struct ignore
    {
        T ignoreItem;
        ignore(T const& x): ignoreItem(x) {}
    };
    template<typename T>
    ignore<T> I(T const& x) { return ignore<T>(x);}  // This is where I in the above code comes from.
    

    Then the input operator>> for the ignore looks like this.

    std::istream& operator>>(std::istream& stream, ignore<T> const& test)
    {
        T   next;
        if ((stream >> next) && (test.ignoreItem != next))       // if the stream already failed
        {    stream.setstate(std::ios::badbit);                 // then don't change anything
        }                                                       // as it may confuse people
        return stream;
    }
    

    A specialization for string. To handle the fact that operator>> on string only reads a word.
    Note: in scanf() a space matches 1 or more spaces. Thus obeys the same rule if the input string has a space in it.

    template<>
    struct ignore<std::string>
    {
        std::vector<std::string> ignoreItemVector;
        ignore(std::string const& x)
        {
            // Split the input into a list of words to ignore.
            std::stringstream  words(x);
            std::copy(std::istream_iterator<std::string>(words),
                    std::istream_iterator<std::string>(words),
                    std::back_inserter(ignoreItemVector)
                    );
        }
    };
    template<>
    std::istream& operator>><std::string>(std::istream& stream, ignore<std::string> const& test)
    {
        // Specifically ignore each word.
        foreach(std::string const& loop, test.ignoreItemVector)
        {
            std::string   next;
            if ((stream >> next) && (loop != next))       // if the stream already failed
            {    stream.setstate(std::ios::badbit);      // then don't change anything
            }                                            // as it may confuse people
        }
        return stream;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider I have a JSON string of the following format: How do I parse
Consider I have the following dictionary: dic={} dic[var1]=val1 dic[var2]=val2 Now I have another object
In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
Consider I have the following text in a UILabel (a long line of dynamic
Consider I have the following interface: public interface A { public void b(); }
From couple of days i am thinking of a following scenario Consider I have
Lets consider that I have a public property called AvatarSize like the following, public
Consider the following situation: We have two Localizable.string files, one in en.lproj and one
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following example. I have an interface MyInterface, and then two abstract classes

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.