By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,432 Members | 790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,432 IT Pros & Developers. It's quick & easy.

Vector Finding and inserting

P: n/a
KL
Well, I am back.

This time our assignment has us filling a vector and then timing how
long it takes to find a spot in the vector to insert a new item, and
the time required to insert the item after that spot is found.

Well, I am kinda lost on this finding things in a vector. I mean,
wouldn't you just go to vector[N], and then ....well then I gotta find
out how to do an insert in a vector. Does this involve changing the
pointer to point to a new entry?

The code I have so far is as follows (don't laugh to hard at any
mistakes your find), it really is formatted better in Visual Studio.
Seems the cut and paste messes it up terribly.:

#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include <windows.h>
#include <fstream>

using namespace std;

int main( ) {
LARGE_INTEGER timeStart, timeEnd, TimerFrequency;
QueryPerformanceFrequency(&TimerFrequency);
double elapsedTime, cycleTime=1.0/(double)TimerFrequency.LowPart;
int frequency = TimerFrequency.LowPart;
cout << endl << frequency << " " << cycleTime << endl;

vector<int> n;
int a, b, sizev, insv;
ofstream vecf;
listb.open("listback.txt", ios::app);
listf.open("listfront.txt", ios::app);
vecf.open("vectorfind.txt", ios::app);
cout << endl << "Loading Vector" << endl;
cout << endl << "Enter size of vector: ";
cin >> sizev;
cout << endl << "Enter number of elements to do a find and insert
on: ";
cin >> insv;
for (a=0; a < sizev; a++) { //fills vector
b = ( (rand()+1)*(rand()+1) ) % a + 1;
v.push_back(b);
}
cout << endl << "Timing finding elements in vector . . ." << endl;
for (a=0; a<insv ; a++){
b = ( (rand()+1)*(rand()+1) ) % sizev + 1;
QueryPerformanceCounter(&timeStart);
vi = find(v.begin(), v.end(), b);
QueryPerformanceCounter(&timeEnd);
elapsedTime =
(double)(timeEnd.LowPart-timeStart.LowPart)*cycleTime;
vecf << elapsedTime << endl;
}
}
vecf.close( );

return 0;
}

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
KL wrote:
Well, I am back.
Oooo... Goosebumps...
This time our assignment has us filling a vector and then timing how
long it takes to find a spot in the vector to insert a new item, and
the time required to insert the item after that spot is found.

Well, I am kinda lost on this finding things in a vector.
Have you tried reading a book about the Standard Library? std::find is
just for that purpose, finding things...
I mean,
wouldn't you just go to vector[N],
What does it mean "to go to vector[N]"?
and then ....well then I gotta find
out how to do an insert in a vector.
Have you tried reading a reference manual? std::vector::insert is just
the function for that. And you know what, it takes an iterator, which
you could get from... std::find, what else? Did you guess right? I
am sure you did.
Does this involve changing the
pointer to point to a new entry?
Nope.
The code I have so far is as follows [..] :
[...]
vi = find(v.begin(), v.end(), b);
Hey, you're doing the right thing! Now, just read about the 'insert',
and you're going to be set!
[..]


V
Jul 23 '05 #2

P: n/a
Ian
KL wrote:
Well, I am back.

This time our assignment has us filling a vector and then timing how
long it takes to find a spot in the vector to insert a new item, and
the time required to insert the item after that spot is found.

Well, I am kinda lost on this finding things in a vector. I mean,
wouldn't you just go to vector[N], and then ....well then I gotta find
out how to do an insert in a vector. Does this involve changing the
pointer to point to a new entry?

Is this real code, or an exercise? I ask because vector isn't the best
container to use if you want to insert in the middle. vector works best
if you are only appending, not inserting. Try a list or a deque.

Ian
Jul 23 '05 #3

P: n/a
KL


Ian wrote:
KL wrote:
Well, I am back.

This time our assignment has us filling a vector and then timing how
long it takes to find a spot in the vector to insert a new item, and
the time required to insert the item after that spot is found.

Well, I am kinda lost on this finding things in a vector. I mean,
wouldn't you just go to vector[N], and then ....well then I gotta find
out how to do an insert in a vector. Does this involve changing the
pointer to point to a new entry?

Is this real code, or an exercise? I ask because vector isn't the best
container to use if you want to insert in the middle. vector works best
if you are only appending, not inserting. Try a list or a deque.

Ian


Thanks all...yes it is an excercise. Actually, it is a class
assignment. I am trying to understand it, but seem lost at times.

KL

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.