Now I am completely confused. I am googling all day and still can't get why this code doesn't work.
I have vector
of structs
and those structs
have string
property. When I want to add a new struct
into vector
, as first I have to check whether a struct
with the same string
property is already there. If it is, it won't be added.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Try{
string name;
Try( string name) : name ( name ) { }
bool operator < ( const Try & a ) const
{
return name < a . name;
}
};
int main(){
vector<Try> vektor;
Try *n;
vektor . push_back( Try( "Prague" ) );
n = new Try( "Brno" );
vector<Try>::iterator it = lower_bound( vektor . begin(), vektor . end(), n -> name);
if( it == vektor . end() ){
cout << "not included" << endl;
cout << it -> name << endl;
}
else
cout << "included" << endl;
return 0;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…