ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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).
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.
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.
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
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
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.