Vector Finding and inserting

K

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;
}
 
V

Victor Bazarov

KL said:
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
 
I

Ian

KL said:
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
 
K

KL

Ian said:
KL said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top