在学习STL时的一些问题 [英] Some Problems while learning STL

查看:111
本文介绍了在学习STL时的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu的CodeBlocks IDE中使用g ++。
我是STL和C ++的一部分的新用户。



Q1://已回答

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

是正确的,但当我更改

  std :: istream_iterator< std :: string>结束; 

  std :: istream_iterator< std :: string>结束(); 

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



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<第一;
}
} firstLess1; };


struct FirstLess:std :: binary_function< PS,PS,bool> {
bool operator()(const PS& p,const PS& q)const {
return p.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,finis,std :: not1 (std :: bind1st(firstLess2,* j))); 

然后它通过编译。



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

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

,编译器没有给我一个错误。

解决方案

std :: istream_iterator< std :: string> end(); C ++将此解释为函数的声明,其名称为 end 返回值类型为 std :: istream_iterator& std :: string> 并且参数列表为空。这就是为什么你得到这样的错误。在C ++中,通过调用它的类的默认构造函数来创建任何对象,你只需要做这个 type_name variable_name; type_name variable_name(); 将被解释为函数声明。


I am using g++ in CodeBlocks IDE in Ubuntu. I am new to STL and some part of C++.

Q1: //answered

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

is correct, but when I changed

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

into

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

the compiler says no matching function call at the fourth line.

Q2: //sorry i did not make the problem clear the first time

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, PS, bool> {
bool operator() ( const PS & p, const PS & q ) const {
    return p.first < q.first;
}} firstLess2;

Note that the only difference between firstLess1 and firstLess2 is that firstLess1 is declared in PS.

when I call the function :

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

the compiler gave me an error 'undefined reference to PS::firstLess1'. and then I changed to

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

then it passed the compile.

More strange, in some other part of the program, i used both

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

and the compiler did not gave me an error.

解决方案

std::istream_iterator< std::string > end(); C++ interpretes this as declaration of function which name is end return value type is std::istream_iterator< std::string > and argument list is empty. Thats why you get such error. In C++ to make any object by calling default constructor of its class you just must do this type_name variable_name;. type_name variable_name(); will be interpreted as function declaration.

这篇关于在学习STL时的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆