Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some Problems while learning STL

Tags:

c++

stl

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.

like image 336
user522829 Avatar asked Feb 25 '26 18:02

user522829


2 Answers

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.

like image 126
Mihran Hovsepyan Avatar answered Feb 28 '26 08:02

Mihran Hovsepyan


A1: already answered sufficiently
A2: add a const to the declaration of firstLess, OR define the firstLess object in a cpp file (see this)

like image 45
smerlin Avatar answered Feb 28 '26 09:02

smerlin