C++:需要有关流的更多帮助
我一直在阅读有关流的内容,但是当我尝试偏离书本来完成任务时,我似乎做错了一些事情。无论如何,这是我的代码的开始。如果此代码可以稍作修改即可工作,请告诉我,否则,如果您可以提供更好的“更像 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 );
}
UPDATE[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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
use
从行中删除 ():
Should be:
Or:
Or else is 是一个返回 istream 的函数。
Remove the () from the line:
Should be:
Or:
Or else is is a function that returns an istream.