C++:需要有关流的更多帮助

发布于 2024-11-27 07:04:25 字数 5059 浏览 0 评论 0原文

我一直在阅读有关流的内容,但是当我尝试偏离书本来完成任务时,我似乎做错了一些事情。无论如何,这是我的代码的开始。如果此代码可以稍作修改即可工作,请告诉我,否则,如果您可以提供更好的“更像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒人说梦 2024-12-04 07:04:25

使用

#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 );
}

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 );
}
ら栖息 2024-12-04 07:04:25

从行中删除 ():

istream is();

Should be:

istream is;

Or:

istream is("text 1.2");

Or else is 是一个返回 istream 的函数。

Remove the () from the line:

istream is();

Should be:

istream is;

Or:

istream is("text 1.2");

Or else is is a function that returns an istream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文