473,847 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vector Finding and inserting

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;
QueryPerformanc eFrequency(&Tim erFrequency);
double elapsedTime, cycleTime=1.0/(double)TimerFr equency.LowPart ;
int frequency = TimerFrequency. LowPart;
cout << endl << frequency << " " << cycleTime << endl;

vector<int> n;
int a, b, sizev, insv;
ofstream vecf;
listb.open("lis tback.txt", ios::app);
listf.open("lis tfront.txt", ios::app);
vecf.open("vect orfind.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)*(ran d()+1) ) % a + 1;
v.push_back(b);
}
cout << endl << "Timing finding elements in vector . . ." << endl;
for (a=0; a<insv ; a++){
b = ( (rand()+1)*(ran d()+1) ) % sizev + 1;
QueryPerformanc eCounter(&timeS tart);
vi = find(v.begin(), v.end(), b);
QueryPerformanc eCounter(&timeE nd);
elapsedTime =
(double)(timeEn d.LowPart-timeStart.LowPa rt)*cycleTime;
vecf << elapsedTime << endl;
}
}
vecf.close( );

return 0;
}

Jul 23 '05 #1
3 2238
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::in sert 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3012
by: JerryJ | last post by:
I have to fetch values from a database and store them in a vector in my c++ application. What is faster? if i fetch the values sorted from the database with a 'order by' clause in the sql-statement or if i fetch them into the vector unsorted and sort the vector then? Thanx for your help
2
2418
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0), an iterator that I've set in a loop gets changed. Here's an example of the problem (sorry about the lack of indentation, posting this from Google): #include <vector> #include <iostream>
5
5563
by: toton | last post by:
Hi, I want to append one vector after another. so like, vector<intv1; ///put some elements to v2. I have a second vector vector<intv2; ///it has some elements. Now I do v1.reserve(v1.size() + v2.size()) ; and then
3
1835
by: edu.mvk | last post by:
Hi, i have the following code which updates the vector of strings( p_vector ) each time it goes into the loop for the first iteration we have only one string in the vector. But in the while loop we are updating the vector and adding few more strings at the end. I'm expecting that In the second iteration the "it" should point to the updated vector, but it is pointing to the "NULL".
1
3961
by: Raghuram N K | last post by:
Hi, Following program compiles and executes successfully in windows with DevCPP compiler. When I compile the same in Linux with 'g++323' compiler I get following assignment error: cannot convert `__gnu_cxx::__normal_iterator<DailyTemp*, std::vector<DailyTemp, std::allocator<DailyTemp >' to `DailyTemp*' in assignment I believe the overloaded assignment operation is unable to recognize the
1
13620
by: tinie | last post by:
I encounter problems in inserting elements in a 2d vector. For example I want to insert 99 in v, how would I do it? What I have is this code: #include <iostream> #include <iomanip> #include <vector> int main() { std::vector< std::vector<int> > v;
12
4922
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back instead I don't need to supply the iterator. But what
3
1486
by: Markus Dehmann | last post by:
Hi, I observed a behavior that I didn't expect: I have a vector of sets. I iterate over the first of these sets, and while I iterate over it, I add another set at the end of the vector. I thought that shouldn't affect the first set I am iterating over, but it does, and I get a segmentation fault. See the example program below.
13
1934
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize this? I wrote the following program to test this out. #include <fstream> #include <iostream> #include <string>
0
10643
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10706
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7879
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7050
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5907
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.