我一直在阅读有关流的信息,但是当我尝试偏离书籍来完成任务时,我似乎做错了什么。无论如何,这是我代码的开始。如果此代码可以稍作修改即可工作,请告诉我,否则,如果您可以提供更好的“更像C ++”的途径,我将不胜感激。
#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 );
}
编译器错误是:
[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]:
删除()后
并更改:
istream& operator>>(istream& stream, pair<string,float> in ) {
至
istream& operator>>(istream& stream, pair<string,float> &in ) {
我得到了不同的编译器错误集:
/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
使用以下代码:
#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 );
}
更新[1]
好吧,我解决了我认为的问题。回到Stroustrup的desk_calculator示例,我有以下至少可以编译的代码,我将向其中添加最终功能,然后将最终产品重新发布给感兴趣的任何人。
#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 );
}
更新[2]
我认为我没有达到问题的要求,但我掌握了技术上的工作。
#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;
}
最佳答案
采用
#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 );
}