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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:33:41+00:00 2026-05-24T06:33:41+00:00

I have been reading about streams, but when I try and deviate from the

  • 0

I have been reading about streams, but when I try and deviate from the books to accomplish a task I seem to be doing something wrong. Anyways here is the start of my code. If this code can work with slight modifications please let me know, else if you could provide a better “more C++ like” route I would greatly appreciate it.

#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

std::multimap<std::string,float> _map;

istream& operator>>(istream& stream, pair<string,float> in ) { 
    return stream >> in.first >> in.second;
}

int main( int argc, char *argv[ ] ) { 
    istream is( );
    do {
        pair<string,float> input;
        is >> input;
        _map.insert(input);
    } while( is );
}

Compiler errors are:

[mehoggan@desktop bjarne_stroustrup]$ g++ -o map -Wall ./key_value_stats.cpp
./key_value_stats.cpp: In function ‘int main(int, char**)’:
./key_value_stats.cpp:20:15: error: no match for ‘operator>>’ in ‘is >> input’
./key_value_stats.cpp:12:10: note: candidate is: std::istream& operator>>(std::istream&, std::pair<std::basic_string<char>, float>)
./key_value_stats.cpp:22:14: warning: the address of ‘std::istream is()’ will always evaluate as ‘true’

UPDATE[0]:

After removing the ( )
and changing:

istream& operator>>(istream& stream, pair<string,float> in ) {

to

istream& operator>>(istream& stream, pair<string,float> &in ) {

I get a different set of compiler errors:

/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../../include/c++/4.5.1/istream: In function ‘int main(int, char**)’:
/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../../include/c++/4.5.1/istream:582:7: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char, _Traits = std::char_traits<char>]’ is protected
./key_value_stats.cpp:17:13: error: within this context

with the following code:

#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

std::multimap<std::string,float> _map;

istream& operator>>(istream& stream, pair<string,float> &in ) {
    return stream >> in.first >> in.second;
}

int main( int argc, char *argv[ ] ) { 
    istream is;
    do {
        pair<string,float> input;
        is >> input;
        _map.insert(input);
    } while( is );
}

UPDATE[1]

Okay I solved the issue I think. Going back to Stroustrup’s desk_calculator example, I have the following code which at least compiles, I will add the final features into it, and then re-post the final product for anyone interested.

#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

std::multimap<std::string,float> _map;

istream& operator>>(istream& stream, pair<string,float> &in ) {
    return stream >> in.first >> in.second;
}

//void print_pair( pair<string,float>

int main( int argc, char *argv[ ] ) { 
    istream *is = &cin;
    do {
        pair<string,float> input;
        (*is) >> input;
        _map.insert(input);
    } while( is );
}

UPDATE[2]

I don’t think I met the requirements of the problem but I got the technical stuff to work.

#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

/********************************************************************
 * Read a sequence of possibly whitespace-separated (name,value)
 * pairs, where the name is a single whitespaace-separated word and
 * the value is an integer or floating-point value. Compute and print
 * the sum and mean for each name and the sum and mean for all names
 * ******************************************************************/

using namespace std;

std::multimap<string,float> _map;

istream& operator>>(istream& stream, pair<string,float> &in ) {
    return stream >> in.first >> in.second;
}

ostream& operator<<(ostream& stream, pair<string,float> &out ) {
    return stream << "(" << out.first 
                  << ", " << out.second << ")" << endl;
}

int main( int argc, char *argv[ ] ) { 
    istream *is = &cin;
    do {
        pair<string,float> input;
        (*is) >> input;
        _map.insert(input);
    } while( is->peek( ) != EOF );

    ostream *os = &cout;
    multimap<string,float>::iterator mit = _map.begin( );
    float sum = 0.0;
    while( mit != _map.end( ) ) {
        pair<string,float> p_pair = (*mit);
        (*os) << p_pair;
        sum+=p_pair.second;
        mit++;
    }
    float mean = static_cast<float>( sum/_map.size( ) );
    (*os) << "Sum: " << sum << " Mean: " << mean << endl;
}
  • 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-24T06:33:42+00:00Added an answer on May 24, 2026 at 6:33 am

    use

    #include <map>
    #include <string>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <iterator>
    
    using namespace std;
    
    std::multimap<std::string,float> _map;
    
    istream& operator>>(istream& stream, pair<string,float>& in ) { 
        return stream >> in.first >> in.second;
    }
    
    int main( int argc, char *argv[ ] ) { 
        do {
            pair<string,float> input;
             cin >> input;
            _map.insert(input);
        } while( cin );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading about dependency properties in several books but all have one
I have been reading about exception handling on the Apple developer docs , but
I have been reading about the syncroot element but I can't find it in
I have been reading about Domain Events and have seen codes from Udi's implementation(
I have been reading about try/finally on MSDN and found out following code. They
I have been reading about streams in Java the past days. After reading quite
I have been reading about this but I have to do some thing. I
I have been reading about the thread-pool pattern and I can't seem to find
i have been reading about proximity security devices through bluetooth, but i am wondering
I have been reading about the differences between Table Variables and Temp Tables and

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.