加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 学习STL时的一些问题

发布时间:2020-12-16 09:31:01 所属栏目:百科 来源:网络整理
导读:我在Ubuntu的CodeBlocks IDE中使用g. 我是STL的新手,也是C的一部分. Q1://回答 std::istream_iterator std::string begin ( dictionaryFile );std::istream_iterator std::string end;std::vector std::string dictionary;std::copy ( begin,end,std::back_
我在Ubuntu的CodeBlocks IDE中使用g.
我是STL的新手,也是C的一部分.

Q1://回答

std::istream_iterator< std::string > begin ( dictionaryFile );
std::istream_iterator< std::string > end;
std::vector< std::string> dictionary;
std::copy ( begin,end,std::back_inserter ( dictionary ) );

是对的,但是当我改变的时候

std::istream_iterator< std::string > end;

std::istream_iterator< std::string > end();

编译器说第四行没有匹配的函数调用.

Q2://对不起,我第一次没有弄清问题

struct PS : std::pair< std::string,std::string > {
PS();
static struct FirstLess: std::binary_function< PS,PS,bool> {
    bool operator() ( const PS & p,const PS & q ) const {
        return p.first < q.first;
    }
} firstLess1; };


struct FirstLess: std::binary_function< PS,bool> {
bool operator() ( const PS & p,const PS & q ) const {
    return p.first < q.first;
}} firstLess2;

请注意,firstLess1和firstLess2之间的唯一区别是firstLess1在PS中声明.

当我调用函数时:

k = std::find_if ( j + 1,finis,std::not1 ( std::bind1st ( PS::firstLess1,*j ) ) );

编译器给了我一个错误’对PS :: firstLess1的未定义引用’.
然后我改为

k = std::find_if ( j + 1,std::not1 ( std::bind1st ( firstLess2,*j ) ) );

然后它通过了编译.

更奇怪的是,在程序的其他部分,我使用了两者

j = std::adjacent_find ( j,PS::firstLess1 );
j = std::adjacent_find ( j,firstLess2 );

并且编译器没有给我一个错误.

解决方法

的std :: istream_iterator< std :: string>结束(); C将此解释为函数声明,其名称是结束返回值类型是std :: istream_iterator< std :: string>和参数列表为空.这就是为什么你会得到这样的错误.在C中通过调用其类的默认构造函数来创建任何对象,您只需要执行此类型名称variable_name;. type_name variable_name();将被解释为函数声明.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读