how to search for a string in a vector of structs

I have a vector of structs.
I need to find the index of a specific struct in a vector by using the date_time string stored in that particular struct.
the only unique parameter i have is the date_time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

struct data_packets
{
	std::string date_time;
	unsigned short packet[8];
};

int main()
{
  vector<data_packets> storage;
  storage.reserve(1000000);

  while(true)
  {  
    data_packets ds;
    ds.date_time = string("12/06/2013,21:49:41.773250");
    ds.packet[0] = 22222; 
  }
   
 // how do i need to find the structure with the specific string.
 // and retrieve the data packets
 // i just need the index

  if (std::binary_search (data_stored.begin(), data_stored.end(), "12/06/2013,21:49:41.773250"))
  {
         std::cout << "found!\n";
  }
  else
  {
	  std::cout << "not found.\n";
  }  

}
Do you understand that the following loop has no any sense and is infinite?

1
2
3
4
5
6
  while(true)
  {  
    data_packets ds;
    ds.date_time = string("12/06/2013,21:49:41.773250");
    ds.packet[0] = 22222; 
  }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iostream>
#include <boost/bind.hpp>
#include <iterator>

using namespace std;

struct data_packets
{
	std::string date_time;
	unsigned short packet[8];
};

struct FindByName {
    const std::string name;
    FindByName(const std::string& name) : name(name) {}
    bool operator()(const data_packets& j) const { 
        return j.date_time == name; 
    }
};

int main()
{
  vector<data_packets> storage;
  storage.reserve(1000000);

  for (int i=0; i < 10; i++)
  {
   data_packets ds;
   stringstream xx;
   xx << "12/06/2013,21:49:41.773250" << i;
   ds.date_time = xx.str();
   ds.packet[0] = 22222 + i;

   storage.push_back(ds);
  }

std::vector<data_packets>::iterator it = std::find_if(
	storage.begin(),
	storage.end(),
    FindByName("12/06/2013,21:49:41.7732501"));

if(it != storage.end()) {
    
	cout << it->date_time << endl;
	cout << it->packet[0] << endl;	
}
else
{
	cout << "not found!" << endl;
}
   
 int x;
 cin >> x;

}
Last edited on
managed to get it to work with boost::bind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <iostream>
#include <boost/bind.hpp>
#include <iterator>

using namespace std;

struct data_packets
{
	std::string date_time;
	unsigned short packet[8];
};

struct FindByName {
    const std::string name;
    FindByName(const std::string& name) : name(name) {}
    bool operator()(const data_packets& j) const { 
        return j.date_time == name; 
    }
};

int main()
{
  vector<data_packets> storage;
  storage.reserve(1000000);

  for (int i=0; i < 10; i++)
  {
   data_packets ds;
   stringstream xx;
   xx << "12/06/2013,21:49:41.773250" << i;
   ds.date_time = xx.str();
   ds.packet[0] = 22222 + i;

   storage.push_back(ds);
  }

std::vector<data_packets>::iterator it;

it = std::find_if(
	storage.begin(),
    storage.end(),
    boost::bind(&data_packets::date_time, _1) == string("12/06/2013,21:49:41.7732502"));

//std::vector<data_packets>::iterator it = std::find_if(
//	storage.begin(),
//	storage.end(),
//    FindByName("12/06/2013,21:49:41.7732501"));

if(it != storage.end()) {
    
	cout << it->date_time << endl;
	cout << it->packet[0] << endl;	
}
else
{
	cout << "not found!" << endl;
}
   
 int x;
 cin >> x;

}\
Topic archived. No new replies allowed.