0
heidik's Avatar, Join Date: Oct 2010
Contributor
Thanks for your advice about how to find out the time complexity.

I meant that:

there are four string type variables in the struct a, x, y, z. I have to find out the difference between member variable 'a' and member variable 'z' of the struct. Since these variables are string type but like "123" so I have to convert them to integers and then find out the difference. By duplicates I mean struct type of object having similar x, y, and z variables (excluding 'a'). After finding the difference I have to put the struct object again to the original vector (which was initially having duplicates), the object which is having minimum difference between 'a' and 'z'. I hope I have explained it well
0
jimblumberg's Avatar
Ambitious contributor
Quote:
Originally Posted by heidik View Post
Thanks for your advice about how to find out the time complexity.

I meant that:

there are four string type variables in the struct a, x, y, z. I have to find out the difference between member variable 'a' and member variable 'z' of the struct. Since these variables are string type but like "123" so I have to convert them to integers and then find out the difference. By duplicates I mean struct type of object having similar x, y, and z variables (excluding 'a'). After finding the difference I have to put the struct object again to the original vector (which was initially having duplicates), the object which is having minimum difference between 'a' and 'z'. I hope I have explained it well
I understand everything above but:
Quote:
After finding the difference I have to put the struct object again to the original vector
Do you mean that you are adding a "record" to the vector or you need to alter the contents of one of the existing "records"?

Also I believe your program is not doing what you want. When do your print out the section titled: "dPred: Only the values that were duplicated" is only storing the first duplicate record. I added another field to your structure to see this. Here is my output.

Quote:
vPred: Sorted input
[a, a, A]
[a, a, C]
[a, a, H]
[a, a, I]
[a, b, B]
[a, b, E]
[a, c, F]
[b, a, D]
[b, b, G]
dPred: Only the values that were duplicated
[a, a, A]
[a, a, A]
[a, a, A]
[a, a, A]
[a, b, B]
[a, b, B]
uPred: Only the values that were unique
[a, c, F]
[b, a, D]
[b, b, G]
Jim
0
heidik's Avatar, Join Date: Oct 2010
Contributor
Hello Jim. I have ran the program again and it is giving me the correct result . The only problem is now the speed. According to what you suggested I tried it first with 10 test values, then with 100 and then with 60000 values and the program almost became DEAD with 60000 values specially when looking for duplicates ;'(. Do you have any idea how can I make it better, fast, only the duplicate part ?

Thanks in advance.
0
heidik's Avatar, Join Date: Oct 2010
Contributor
Hello Jim. I have run the program again and it is giving me the correct result . The only problem is now the speed. According to what you suggested I tried it first with 10 test values, then with 100 and then with 60000 values and the program almost became DEAD with 60000 values specially when looking for duplicates ;'(. Do you have any idea how can I make it better, fast, only the duplicate part ?

Thanks in advance.
0
jimblumberg's Avatar
Ambitious contributor
The only thing I can suggest is to change your dPred and uPred vectors to pointers.

Code:
	std::vector<MyPred*> uPred; // values that were always unique
	std::vector<MyPred*> dPred; // values that were duplicated
This will mean instead of copying all the duplicates to a new vector you will only push the pointer to the current record into the new vector. This should speed things up quite a bit.

You will have to change the code where you push_back() into the new vector.

Code:
dPred.push_back(&(*j));  //put each duplicate onto a new vector
And also where you print them out.

Hope this helps.

Jim
0
heidik's Avatar, Join Date: Oct 2010
Contributor
Alright Thank you Jim. I have the changes you suggested and it is working. I actually need the uPred (unique elements) and dont need dPred and vPred so I made them pointer type and deleted them in the end.

Could you please help me complete this assignment. I am actually reading from a file containing strings (lines). The first and last string elements of each line are start date/time and end date/time respectively. I have to find the duplicates in original vector where all string elements in a line are the same except the first element i-e start date/time. I have to find the difference between the minits of the start date/time and the minits of the end date/time and push_back the element from the duplicates to the original vector where the time difference is the minimum. e-g

Start date/time of '1' element is 2006/06/01 16:34:43
End date/time of that element is 2006/06/01/ 16:55: 51

Start date/time of duplicate element (of '1') is 2006/06/01 16:24:43
End date/time of that element is 2006/06/01/ 16:55: 51

I now have to find the difference between 34 (minits of '1' element's start date/time) and 55 (minits of '1' element's end date/time) which is equal to 21

and the difference between 24 (minits of duplicate (of '1') element's start date/time) and 55 (minits of duplicate (of '1') element's end date/time) which is equal to 31.

since 21 is less than 31 so I will push_back the element with time difference of 21 to the original vector and will discard all its duplicates.

Could you help me do this? I hope I have explained well. For the time being I have put random int+string combination to struct first (start date/time) and last (end date/time) member variables instead of proper date/time to find the difference between the int part of that variable. Here's my code

Code:
struct MyPred
{
	std::string a;
	std::string x;
	std::string y;
	std::string z;

	MyPred(const std::string& a, const std::string& x, const std::string& y, const std::string& z): a(a), x(x), y(y), z(z) {}

	bool operator==(const MyPred& p) const
	{
		return x == p.x && y == p.y && z == p.z; // a == p.a && 
	}

	bool operator<(const MyPred& p) const
	{
		//if(a < p.a) return true;
		//if(a > p.a) return false;
		if(x < p.x) return true;
		if(x > p.x) return false;
		if(y < p.y) return true;
		if(y > p.y) return false;
		if(z < p.z) return true;
		if(z > p.z) return false;
		return false;
	}
};


int main()
{
	std::vector<MyPred>* vPred = new std::vector<MyPred>;
	vPred->push_back(MyPred("a2c", "1Gak", "c", "d4f"));
	vPred->push_back(MyPred("j4h", "b", "c", "j87h"));
	vPred->push_back(MyPred("d4f", "1Gak", "c", "d4f"));
	vPred->push_back(MyPred("n7s", "1Gak", "c", "d4f"));
	vPred->push_back(MyPred("l9m", "b", "c", "j87h"));
	vPred->push_back(MyPred("p24a", "x", "c", "p43a"));
	vPred->push_back(MyPred("q56r", "l", "m", "q90r"));
	vPred->push_back(MyPred("g11v", "8f", "h", "g63v"));
	vPred->push_back(MyPred("u3w", "v", "d", "u11w"));
	vPred->push_back(MyPred("k76l", "x", "c", "p43a"));
	vPred->push_back(MyPred("p24a", "g", "z", "p43a"));

	// The values need to be in order for equal_range() to work
	std::sort(vPred->begin(), vPred->end());

	std::vector<MyPred> uPred; // values that were always unique
	std::vector<MyPred>* dPred = new std::vector<MyPred>; // values that were duplicated

	std::pair<std::vector<MyPred>::iterator, std::vector<MyPred>::iterator> ret;

	for(std::vector<MyPred>::iterator i = vPred->begin(); i != vPred->end(); i = ret.second)
	{
		/*ret = std::equal_range(i, vPred.end(), *i);
		if(ret.second - ret.first == 1)
		{
			uPred.push_back(*i);
		}
		else
		{
			dPred.push_back(*i);
		}*/

		ret = std::equal_range(i, vPred->end(), *i);
		
		if(ret.second - ret.first != 1) // duplicates
		{
				for(std::vector<MyPred>::iterator j = ret.first; j != ret.second; ++j)
				{
					dPred->push_back(*j); //put each duplicate onto a new vector
				}
		}
		else if(ret.second - ret.first == 1)
		{
			uPred.push_back(*i);
		}
	}

	std::cout << "vPred: Sorted input\n";
	for(std::vector<MyPred>::iterator i = vPred->begin(); i != vPred->end(); ++i)
	{
		std::cout << "[" << i->a << ", " << i->x << ", " << i->y << ", " << i->z << "]" << '\n';
	}

	std::cout << "dPred: Only the values that were duplicated\n";
	for(std::vector<MyPred>::iterator i = dPred->begin(); i != dPred->end(); ++i)
	{
		std::cout << "[" << i->a << ", " << i->x << ", " << i->y << ", " << i->z << "]" << '\n';
	}

	std::cout << "uPred: Only the values that were unique\n";
	for(std::vector<MyPred>::iterator i = uPred.begin(); i != uPred.end(); ++i)
	{
		std::cout << "[" << i->a << ", " << i->x << ", " << i->y << ", " << i->z << "]" << '\n';
	}

	delete vPred;
	delete dPred;
}
0
jimblumberg's Avatar
Ambitious contributor
Quote:
Start date/time of '1' element is 2006/06/01 16:34:43
End date/time of that element is 2006/06/01/ 16:55: 51

Start date/time of duplicate element (of '1') is 2006/06/01 16:24:43
End date/time of that element is 2006/06/01/ 16:55: 51

I now have to find the difference between 34 (minits of '1' element's start date/time) and 55 (minits of '1' element's end date/time) which is equal to 21

and the difference between 24 (minits of duplicate (of '1') element's start date/time) and 55 (minits of duplicate (of '1') element's end date/time) which is equal to 31.

since 21 is less than 31 so I will push_back the element with time difference of 21 to the original vector and will discard all its duplicates.

For this you should check out the functions in the ctime header.

Create a structure tm to hold each of the dates and times. Then use difftime to compute the difference between the start and end times. I would not forget about the date part because if your time passes over midnight you will get incorrect results.

I would use stringstream to break apart your date, time string.

Jim

Last edited by jimblumberg; 24Oct2010 at 19:50.. Reason: Added link
0
heidik's Avatar, Join Date: Oct 2010
Contributor
Pleaseeeeee Jim could you please do it for me because someone else suggested me the same thing but I havent used ctime etc ever. I have to submit it tomorrow ;'(. Pleaseeeeeeeeeeeee. Could you please make changes to my code and do it for me. I shall really be grateful to you.
0
jimblumberg's Avatar
Ambitious contributor
Quote:
Originally Posted by heidik View Post
Pleaseeeeee Jim could you please do it for me because someone else suggested me the same thing but I havent used ctime etc ever. I have to submit it tomorrow ;'(. Pleaseeeeeeeeeeeee. Could you please make changes to my code and do it for me. I shall really be grateful to you.
I will help you, but I will not do the assignment for you. If I just give you the code what will you learn??

Create a function returning the difference in time (int) with the parameters of the start time, and end time strings.

The first thing to do is to strip out the individual parts ( year, month, day, hours, minutes, seconds. You can use std::string.substr() to do this. Or you could use getline() your choice.

Convert the individual strings that you obtained above to ints.

Create a structure tm for start and end times. Here is what struct tm looks like: http://www.cplusplus.com/reference/clibrary/ctime/tm/

Put the individual ints into the correct element of your structures.

Use mktime() to make the times from your structures. http://www.cplusplus.com/reference/c.../ctime/mktime/

Then use difftime() to compute the differences. http://www.cplusplus.com/reference/c...time/difftime/

Jim
0
heidik's Avatar, Join Date: Oct 2010
Contributor
it isnt a university assignment. It has been given to me in office ;'(. I am new to STL and the code they have given me uses STL and it has almost been 2 weeks since I am trying to do it. I had to understand that code first and then tried to make changes and it has taken me almost 2 weeks. I just want to finish it before my boss says something to me ;'(. Could you please help?