LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2007, 11:35 AM   #1
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
STL lower_bound


Hello. I am trying to insert elements into an stl vector and I am trying to do it in such a way that it's permanently sorted. So I figured I could use lower_bound, and insert the element there (I know, some may say that if I need to insert elements in the middle of the array, I should choose lists, but I need random access to elements).

So I said

PHP Code:

bool client_comp
(client *a,client *b)
{
    return(
a->GetId()<b->GetId());
}

....

lower_bound(v.begin(),v.end(),cl,client_com); 
This compiles fine. But the problem is I can't actually get the position. No matter to whom (an integer, an iterator, etc) I assign the value returned by lower_bound, the compiler throws a very descriptive error (yeah, I love stl errors).

I googled for answers, but it seems, there isn't an awful lot of info on this topic.. Everywhere I turn to, people use some vector<int> to demonstrate the issue.

Last edited by nc3b; 07-12-2007 at 11:36 AM.
 
Old 07-12-2007, 01:42 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
what exact code is causing the error? what is the error?
 
Old 07-12-2007, 02:34 PM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
int a = lower_bound(v.begin(),v.end(),cl,client_com);

produces

Code:
cannot convert `__gnu_cxx::__normal_iterator<client**, std::vector<client*, std::allocator<client*> > >' to `int' in initialization
int *a = lower_bound(v.begin(),v.end(),cl,client_com);

produces a similar error..
 
Old 07-12-2007, 05:23 PM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by nc3b
int a = lower_bound(v.begin(),v.end(),cl,client_com);

produces

Code:
cannot convert `__gnu_cxx::__normal_iterator<client**, std::vector<client*, std::allocator<client*> > >' to `int' in initialization
int *a = lower_bound(v.begin(),v.end(),cl,client_com);

produces a similar error..
They are the correct errors are they not? they are telling you that function returns an iterator and you are trying to convert to an int and int*.

[edit]
Understanding template errors is learning from past mistakes, I think it would be beneficial if we just spilt that error down so you can look at it.

Code:
cannot convert
Code:
`__gnu_cxx::__normal_iterator<client**, std::vector<client*, std::allocator<client*> > >'
to
Code:
`int'
Code:
in initialization
the following is probably the line that throws some confusion
Code:
_gnu_cxx::__normal_iterator<client**, std::vector<client*, std::allocator<client*> > >'
An iterator is a pointer to an element and you have a structure(vector) of client*, and the container is using the default allocator(probably) for the element type.
gcc doc for lower_bound
Quote:
template<typename _ForwardIterator, typename _Tp, typename _Compare>
_ForwardIterator std::lower_bound ( _ForwardIterator __first,
_ForwardIterator __last,
const _Tp & __val,
_Compare __comp
)
hope it helps

Last edited by dmail; 07-13-2007 at 02:28 PM.
 
Old 07-12-2007, 07:04 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Can you use a std::map or std::multimap? Those are both associative, but it might be easier to make the order-determining value the key for each element and let the container automatically sort. Or std::set/std::multiset.
ta0kira

Last edited by ta0kira; 07-12-2007 at 07:17 PM.
 
Old 07-13-2007, 01:33 AM   #6
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
I finally made it work with an iterator. At the same point, for some external reason, I decided I can't automatically sort the vector. Thank you all for your answers.
 
Old 07-13-2007, 02:45 PM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
As taOkira points out there maybe a better structure to use possibly a self balancing tree?
Here is a link that could be useful for future reference.

Did you end up at set?

Last edited by dmail; 07-13-2007 at 02:50 PM.
 
Old 07-14-2007, 08:47 AM   #8
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Thank you very much. I am really interested about changing my vectors to maps or sets.

First I must explain my last post. I have a function which adds elements of type client* to an array-like structure (currently this is a vector, in the next few hours it might very well turn into a map or set).

The thing is that at the time when I am adding the element to the set, I don't have it's id. This id is an integer I have to retrieve from a postgres database, after someone, through the network provides some info (login,password) and I decided I will run the query and fetch the result in another thread, so it won't block the rest of the program.. Only then, in that thread can I set the id of the element.

You may ask why do I have to add the client to the array before running the thread.. As you may have figured, this is a network server and I am polling on some file descriptors so it won't block. When some info comes on one of the descriptors, I have to search through some array, to figure out which client said what. So the client must already be in the list, so I can find him when he provides the user and password and set his id..

Now I am thinking of using a separate array for "temporary" users, who haven't yet authenticated themselves. Do you think this is a good idea?
 
Old 07-16-2007, 03:18 AM   #9
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Hello. I did change stuff in my program, and now it works very nice with maps . The problem was I hadn't really understood much about stl (not that I do now, but at least I know more than before ). Thank you very much.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hello from STL oakleyd LinuxQuestions.org Member Intro 1 12-15-2006 11:03 PM
STL online help available ? carcassonne Programming 3 07-12-2006 05:49 PM
STL 2D Array lramos85 Programming 5 07-12-2006 05:34 PM
what will be the return value? ( STL and c++) ankit4u1 Programming 3 06-15-2006 01:59 PM
c++ and stl G67 Programming 4 12-17-2003 02:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration