473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert to a vector quickly and get the insertation position

Hi,
I have an sorted vector<doublev and want to insert a new double val.
What I need is the position the val have been inserted at in the
vector. My basic idea is something like this (pseudo-code) :

it = lower_bound(v.b egin(), v.end(), greater(val));
v.insert(it,val );
size_t pos = it - v.begin();

Does anyone know whether lower_bound finds the first occurrence of an
object in a vector in O(log(n)) in case the values in the vector are
unique?
Are there any better ideas how to do it easy and most important fast?

Thanks in advance,
Thomas Kowalski

Sep 22 '06 #1
4 2786

Thomas Kowalski wrote:
Hi,
I have an sorted vector<doublev and want to insert a new double val.
What I need is the position the val have been inserted at in the
vector. My basic idea is something like this (pseudo-code) :

it = lower_bound(v.b egin(), v.end(), greater(val));
v.insert(it,val );
size_t pos = it - v.begin();

Does anyone know whether lower_bound finds the first occurrence of an
object in a vector in O(log(n)) in case the values in the vector are
unique?
Are there any better ideas how to do it easy and most important fast?
Well first of all, you use your iterator after the insert: you can't as
the iterator might be invalidated. Secondly, the search-time will be
O(log n), but the time to insert is O(1). You probably use the wrong
container (I propose std::set), but that depends on your needs.

/Peter

Sep 22 '06 #2
Thomas Kowalski wrote:
I have an sorted vector<doublev and want to insert a new double val.
What I need is the position the val have been inserted at in the
vector. My basic idea is something like this (pseudo-code) :

it = lower_bound(v.b egin(), v.end(), greater(val));
What's the "greater" for?
v.insert(it,val );
size_t pos = it - v.begin();

Does anyone know whether lower_bound finds the first occurrence of an
object in a vector in O(log(n)) in case the values in the vector are
unique?
Are there any better ideas how to do it easy and most important fast?
Insertions in a vector are notoriously slow. Consider (a) reserving the
room by calling 'reserve' if you know how many elements your vector will
contain when you complete all insertions, (b) not writing separate
statements - the compiler may or may not be able to optimize them, so
use a single expression like

size_t pos = v.insert(lower_ bound(v.begin() , v.end(), val), val)
- v.begin();

(c) if you need to hold onto the iterator - use ther return value of
'insert', and not from 'lower_bound'.

It may be faster to insert all of the values and then re-sort.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 22 '06 #3
Well first of all, you use your iterator after the insert: you can't as
the iterator might be invalidated.
Right, thanks for mentioning it.
Secondly, the search-time will be O(log n), but the time to insert is O(1).
Don't you mean that the insertation time will be O(n) ?
You probably use the wrong container (I propose std::set),
but that depends on your needs.
Thanks again, but since I need to optimize other methodes primarily, I
guess the vector is the best tradeoff in overall speed.

Regards,
Thomas Kowalski

Sep 22 '06 #4
Hi Victor,
it = lower_bound(v.b egin(), v.end(), greater(val));

What's the "greater" for?
Ok, I just check the docu again and I guess I don't need it.
Insertions in a vector are notoriously slow. Consider (a) reserving the
room by calling 'reserve' if you know how many elements your vector will
contain when you complete all insertions,
There is just one insertation. Probably I am "over-optimizing" this
methode since it actually don't really need to be fast, but this is
mainly for educatiuonal purpose.
(b) not writing separate
statements - the compiler may or may not be able to optimize them, so
use a single expression like

size_t pos = v.insert(lower_ bound(v.begin() , v.end(), val), val)
- v.begin();
Thanks for the tip.
Thanks,
Thomas Kowalski

Sep 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2841
by: Atz | last post by:
Hi to all ! Im using MySql front and PHP 5 for some web shop. I didn't try it so far but i guess that the online data insertation ( accept costs and time ) should be the same process like for local connection. Problem: I have to create online ( on remote server ) some 6 tables and some 20 fields with MySql front. Ok. This shouldn't be great problem ( i hope :-)).
2
5593
by: Steven C | last post by:
vector <int> vecThe; set <int> setThe; vecThe.insert (setThe.begin (), setThe.end ()); This gives an error about iterators not of the right type. Is there a way around this or do I have to set up a for loop and do vecThe.push_back ()? However I can do this:
10
3242
by: halfsearch2 | last post by:
Hi all, I want to sort a bitset vector ( bit 1 > bit 0 ), while it seems bitset doesn't have < or > operation. I tried to use a loop to compare each bit one by one but it's unbearably slow. Any suggestion?
8
4175
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
6
6532
by: Eric Lilja | last post by:
Consider the following code: #include <iostream> #include <string> #include <vector> using namespace std; int main() {
11
1991
by: kaferro | last post by:
I cannot relieve that I am working on Thanksgiving, but I am stuck over something that should be simple. I have a vector that has twenty-four elements. I need to remove the four elements whose positions equal zero. The code below only removes two of the four. Any ideas? logfile << "\n\n\n. . .removing flat positions"; logfile << "vector size=" << componentPerformance.size() << endl; int position=0;
1
13593
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;
5
1807
by: pwalker | last post by:
Hi everyone, I am trying to get my head around iterators used on vectors. Let's take the code below: ------------- std::vector<intv1; std::vector<intv2; v1.push_back( 1 );
10
2190
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which has string value. I really do not understand why push_back() function is trying to remove previously inserted data. Thanks for any help
0
8845
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
8743
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
8522
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
8622
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
5647
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
4173
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
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
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.