vectors

If I am using vectors and want to insert integers from 0, 999,999, and then find the elements with value 100,000 and lace an iterator to point to that element, and then erase the element that was found (element 100,000. how do I create the coding for that.

Also if I want to measure the CPU time how would I go about doing that.
First variant:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>

using namespace std;


int main()
{
	clock_t t = clock();

	vector<int> v(1000000);
	for(vector<int>::size_type i = 0; i < v.size(); i++)
		v[i] = i;
	v.erase(lower_bound(v.begin(), v.end(), 100000));

	t = clock() - t;
	cout << (double)t / CLOCKS_PER_SEC << " sec" << endl;

	return 0;
}

Output: 0.123 sec

Second variant:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>

using namespace std;


int main()
{
	clock_t t = clock();

	vector<int> v;
	for(vector<int>::size_type i = 0; i < 1000000; v.push_back(i++));
	v.erase(find(v.begin(), v.end(), 100000));

	t = clock() - t;
	cout << (double)t / CLOCKS_PER_SEC << " sec" << endl;

	return 0;
}

Ouput: 0.508 sec
Last edited on
If you're using Windows, the way to get the CPU time, rather than the clock time, is using GetThreadTimes(). But I have no idea how you do the same on Linux, etc.
Topic archived. No new replies allowed.