string vector compare

Hi,

I'm trying to compare the content of two string vectores:

1
2
3
    
std::vector<std::string> vect_auxi; 
std::vector<std::string> vect_id;


I tried like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
if (!vect_id.empty()){

size_t found;

for (size_t a = 0; a < vect_aux.size(); a++){
 for(size_t r = 0; r < vect_id.size(); r++){
     found=vect_id[r].find_first_of(vect_aux[a*2]);
     if (found!=string::npos){
          comp = comp + 1;
          EV << "comp: " << comp << '\n';}
      }
  }
}


I also tried like this:

1
2
3
4
5
6
7
8
9
10
if (!vect_id.empty()){

for (size_t a = 0; a < vect_aux.size(); a++){
 for(size_t r = 0; r < vect_id.size(); r++){
   if (vect_id[r].compare(vect_auxi[a*2]) == 0){
          comp = comp + 1;
          EV << "comp: " << comp << '\n';}
      }
  }
}


I do not have an compilation error but the program when running close always here. The problem is in both cases the compare of the content of this string vectors.

Can someone advise me.

Regards,
CMarco
If the vectors are sorted, you can check the size and test each element.
Last edited on
Use equal:

http://www.cplusplus.com/reference/algorithm/equal/

like so:
1
2
3
bool is_equal = false;
if(vect_id.size() == vect_aux.size())
  is_equal = std::equal(vect_id.begin(), vect_id.end(), vect_aux.begin());
1
2
3
for (size_t a = 0; a < vect_aux.size(); a++){
 for(size_t r = 0; r < vect_id.size(); r++){
   if (vect_id[r].compare(vect_aux[a*2]) == 0){}

when 'a' become > vect_aux.size() / 2 - you go out of range of (vect_aux[a*2]
for example (vect_aux have 4 elements) when 'a' become 3 -> vect_aux[a*2] == vect_aux[6] is wrong

try to understand next code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	std::vector< std::string > a; // you first vector ["asd","sdf"]
	a.push_back( "asd" ); 
	a.push_back( "sdf" );
	std::vector< std::string > b; // you second vector ["sdf","asd"]
	b.push_back( "sdf" );
	b.push_back( "asd" );
	std::vector< std::string > c; // additional vector ["sdf","asd"]
	c.push_back( "sdf" );
	c.push_back( "asd" );
	std::cout << (a == c) << std::endl; // print "0" ( ["asd","sdf"] != ["sdf","asd"] )
	std::cout << (b == c) << std::endl; // print "1" ( ["sdf","asd"] == ["sdf","asd"] )
	std::set< std::string > a_s;
	std::set< std::string > b_s;
	std::copy( a.begin(), a.end(), std::insert_iterator< std::set< std::string > >( a_s, a_s.begin() ) ); // copy 'a' elements to set
	std::copy( b.begin(), b.end(), std::insert_iterator< std::set< std::string > >( b_s, b_s.begin() ) ); // copy 'b' elements to set
	std::cout << (a_s == b_s) << std::endl; // print "1"; now a_set == b_set 
Last edited on
No need!! I will try to skip insults and complaits, but, anyway, just use the regular comparison operators, like <, > ,<=, >=, == and !=!
Hi,

the first thing I tried was the regular expressions but this are string vectors, so like the above we can not do:


if (!vect_id.empty()){

for (size_t a = 0; a < vect_aux.size(); a++){
for(size_t r = 0; r < vect_id.size(); r++){
if (vect_id[r] == vect_auxi[a*2]){
comp = comp + 1;
EV << "comp: " << comp << '\n';}
}
}
}
[/code]

I understand completely all your points, maybe a length problem... analyzing...

Regards,
Topic archived. No new replies allowed.