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