473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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.begin(), 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 2769

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.begin(), 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.begin(), 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.begin(), 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
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...
2
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...
10
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...
8
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
by: Eric Lilja | last post by:
Consider the following code: #include <iostream> #include <string> #include <vector> using namespace std; int main() {
11
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...
1
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...
5
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.