sub string comparison

Hello everyone.

I am reading strings from file and I have to identify strings that are similar to eachother but on the basis of a substring i-e if a substring of the two/more strings match then the two/more strings would be considered similar.

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
istringstream ss(line);
getline(ss,ordb.lineID,','); 
getline(ss,ordb.dateTime,',');
getline(ss,ordb.remSTr); 

// Check if the "lineID + remSTr" combination is already in the vector.
// If it is, compare dates and use the lesser one


// how do I compare a substring here? the substring for comparison consists of lineID and remSTr. If lineID and remSTr are similar then the two/more strings would be similar.
vector<ordRecvblock>::iterator it = find(OrdTime.begin(),OrdTime.end(),ordb);

if (it != OrdTime.end())
{
      if (ordb.dateTime < it->dateTime)
      {
            cout << it->lineID << ',' << it->dateTime << ',' << it->remSTr << endl;

            *it = ordb;
      }
      else
      {
            cout << ordb.lineID << ',' << ordb.dateTime << ',' << ordb.remSTr << endl;
      }
}
/*else
{
      OrdTime.push_back(ordb);
}*/
The code below works fine when there is just one similar string to a particular string (comparison done on the basis of substring as mentioned in previous thread) but it doesn't work when there are more than just one similar string to a particular string. And also at which point in the code can I print those strings which do not have matching strings at all? Could anyone please help?

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
istringstream ss(line);

getline(ss,ordb.lineID,',');
getline(ss,ordb.dateTime,',');
getline(ss,ordb.remSTr);

// Check if the "lineID + remSTr" combination is already in the vector.
// If it is, compare dates and use the lesser one

vector<ordRecvblock>::iterator it = find(dupOrdTime->begin(),dupOrdTime->end(),ordb);

if (it != dupOrdTime->end())
{
		    if (ordb.dateTime < it->dateTime)
		    {
					cout << "adding : " << ordb.lineID << ',' << ordb.dateTime << ',' << ordb.remSTr << endl;
					*it = ordb;
		    }
		    /*else ???? ones which do not have any similar strings.
		    {
					cout << "not adding: " << it->lineID << ',' << it->dateTime << ',' << it->remSTr << endl;
		    }*/
}
else
{
		dupOrdTime->push_back(ordb);
}
you can do that by overloading the == (<) operator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class ordRecvblock // assuming that 'ordRecvblock' is actually a class
{
...
  bool operator==(const ordRecvblock &r) const // for '(std::)find()'
  {
    return ((lineID == r.lineID) && (remSTr == r.remSTr));
  }

  bool operator<(const ordRecvblock &r) const // for '(std::)sort()' (if you want to use it)
  {
    return (dateTime < r.dateTime));
  }
...
};
(if I get that right?)
Last edited on
Yes I have already written these overloaded methods but it still didnt work. Or am I not making use of it at all? Please correct me.

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
struct ordRecvblock
{
		std::string lineID;    // alert number
		std::string dateTime;  // date time part
	    	std::string remSTr;    // remaining line

	    	ordRecvblock()
	    	{}

	    	ordRecvblock(const std::string& _lineID, const std::string& _dateTime, const std::string& _remSTr):
	    	lineID(_lineID), dateTime(_dateTime), remSTr(_remSTr)
	    	{}

	    	bool operator==(const ordRecvblock& p) const	// each field except dateTime must be equal
	    	{
			return lineID == p.lineID && remSTr == p.remSTr;
	    	}

	    	bool operator<(const ordRecvblock& p) const	// vector sort
	    	{
			if(remSTr < p.remSTr) return true;
			if(remSTr > p.remSTr) return false;

			return false;
	    	}
	};
so find is supposed to work, isn't it?

Your 'operator<()' doesn't work since you're comparing 'remSTr' and not 'dateTime'. But the comparison of date/time as a string won't work I'd recommend boost::date_time for that purpose. http://www.boost.org/doc/libs/1_45_0/doc/html/date_time.html

Anyway you have to convert that date/time string to a date/time struct/class for calculations
Last edited on
Alright thanks Coder777

But it isnt working for finding many similar strings. It works for finding just two. Is there any particular reason for it?
well, finding more then one requires a loop. like

1
2
3
4
5
6
7
8
9
10
  for(vector<ordRecvblock>::iterator it = dupOrdTime->begin(); it != dupOrdTime->end(); ++it)
  {
    it = find(it,dupOrdTime->end(),ordb);
    if(it != dupOrdTime->end())
    {
       // similar found do somthing...
    }
    else
      break;
  }
it's not tested though

And also at which point in the code can I print those strings which do not have matching strings at all?
after line 25 you know there're not similar. so your output is supposed to be there. With that loop it's a bit more complicated. Use a bool then
Last edited on
Coderrrr7777 it isnt helping ;'(
Sorry?
Topic archived. No new replies allowed.