473,722 Members | 2,484 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 2232
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
3001
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
2411
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
5558
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
1828
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
3959
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
13602
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
4911
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
1478
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
1923
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
8863
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9238
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...
0
9088
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
8052
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
6681
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
5995
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
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.