开发者

Some Problems while learning STL

开发者 https://www.devze.com 2023-01-27 01:57 出处:网络
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 );

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开发者_JS百科();

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.


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


A1:

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

[ Note: since () is not permitted by the syntax for initializer,

X a ();

is not the declaration of an object of class X, but the declaration of a function taking no argument and returning an X.

The form () is permitted in certain other initialization contexts (5.3.4, 5.2.3, 12.6.2). — end note ]

A2:

I'm not really sure what you want, std::find_if and std::adjacent_find do something comepletely different. But anyway, here is some working code inspired by yours.

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

typedef std::pair<std::string, std::string> TwoStrings;

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

int main()
{
    std::vector<TwoStrings> v;
    std::vector<TwoStrings>::iterator it;
    v.push_back(TwoStrings("4. Here's ", "Johnny"));
    v.push_back(TwoStrings("1. Here's ", "Johnny"));
    v.push_back(TwoStrings("2. Here's ", "Johnny"));
    v.push_back(TwoStrings("2. Here's ", "Johnny"));
    v.push_back(TwoStrings("3. Here's ", "Johnny"));

    it = std::adjacent_find(v.begin(), v.end(), FirstLess());
    std::cout << it->first << it->second << std::endl;

    it = std::find_if(v.begin() , v.end(), std::not1(std::bind1st(FirstLess(), *(v.begin()))));
    std::cout << it->first << it->second << std::endl;
}

A the output is:

1. Here's Johnny
4. Here's Johnny
0

精彩评论

暂无评论...
验证码 换一张
取 消