Frage

I have gone through some tutorials on CList, which seems to be much more feature rich than std::list of C++ STL. Especially what caught my attention was that I could search a CList by a value and it would return a position (assuming all values are unique, which is always true in my case). I am trying to use a list of boost smart pointers. Querying a list with the value of the pointer or quickly changing the position value to jump to an element would be really handy to me. Is there any specialized list library that allows me to do that?

Thanks in advance!

War es hilfreich?

Lösung

I wouldn't necessary call MFC more feature rich over STL but it certainly favors the design of member methods and inheritance over free functions and template interfaces.

You can search elements using the functions in algorithm such as std::find, std::find_if, etc which work generically on STL containers like vector as well.

For your specific case you might want to do something like this:

#include <list>
#include <algorithm>

typedef std::list<int> IntList;
int nums[] = { 1,2,3,4,5,6,7,8,9 };
IntList numbers(nums, nums + sizeof(nums) / sizeof(int));
IntList::const_iterator found = std::find(numbers.begin(), numbers.end(), 7);
if (found != numbers.end())
    ;// then found is an iterator to the element containing the value 7
else
    ;// didn't find

Now as far as getting MFC like behavior on Linux I would recommend Qt. It's a massive cross-platform framework which has lots of useful stuff. Better yet it's open source and free and comes with it's own IDE called QtCreator which isn't half bad either.

Andere Tipps

Yes, there is. It's in the standard header <algorithm>, namely the function template std::find.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top