Jump to content
  • Advertisement
Sign in to follow this  
random_thinker

C++ Searching...best STL Algorithm

This topic is 4038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Trying to locate the most efficient searching algorithm... Looking into the C++ Standard library, the standard sequence container search algorithms all have linear complexity, whereas the associated data container (map) has logarithmic complexity. For basic data searching, is it better to use just to use a map and search the key (leaving the value empty) or better to use a sequence container (vector, list, deque)? --random

Share this post


Link to post
Share on other sites
Advertisement
If you have a sorted sequence container you can use std::lower_bound() or std::upper_bound() which are also log(n) operations. But if you use a std::map without a value, you might as well use a std::set. If you never need the elements to be ordered, you also might look into non-standard hash table containers.

Share this post


Link to post
Share on other sites
Depends on the context. What are you trying to do?

If you can sort the data you can use a binary search, for example.

Share this post


Link to post
Share on other sites
What I want to do is to generate a sequence of values one by one and as each new one arrives check it against the others, to see if there are duplicates. There will not be any other use for the data; but I must keep the data in precisely the same order as it is produced (it cannot be sorted).

Otherwise, if more efficient I could keep an 'image' of the original data pattern and use a parallel sorted set. for my purposes.

--random

Share this post


Link to post
Share on other sites
If there is no other use for the data, why keep it in sequence? Anyways, consider using a hash_set, if your compiler comes with an implementation or a set otherwise.

Share this post


Link to post
Share on other sites
Quote:

Otherwise, if more efficient I could keep an 'image' of the original data pattern and use a parallel sorted set. for my purposes.


That sounds like a good idea - you could use a std::vector or std::deque of values for your result and a std::set to check whether the values already exist. If that isn't fast enough you could use a hash table, but there isn't one in the C++ standard library.

Share this post


Link to post
Share on other sites
I also have considered using a map or set that does not accept duplicate values. Then any insertion failure could used to confirm duplication. But is this efficient?

--random

Share this post


Link to post
Share on other sites
Quote:
Original post by random_thinker
But is this efficient?

"Efficient" is a relative term. Inserting n values into a std::set takes O(n log n) time. Inserting n values into a std::hash_set takes about O(n) time, assuming an acceptable hash.

Share this post


Link to post
Share on other sites
Si...looking at this a bit more, I probably only need to keep an image of a moving group of 3 to 5 values. But the entire population would be needed in some other form for checking duplicates.

I could simply use:


unsigned long value = 1010101010UL;
std::set<unsigned long> mySet;
if (!mySet.insert(value).second) std::cout << "Duplicate of " << value << " found!";


And then maintain the ordered sequence image of 3 to 5 values in parallel, no?

--random

Share this post


Link to post
Share on other sites
**fanfare for boost::multi_index_container**


#include <algorithm>
#include <iostream>
#include <iterator>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/identity.hpp>

namespace bmi = boost::multi_index;

template<typename Value>
class unique_list
{
private:
typedef bmi::multi_index_container
<
Value,
bmi::indexed_by
<
bmi::ordered_unique<bmi::identity<Value> >,
bmi::sequenced<>
>
>
container_type;

public:
typedef typename container_type::template nth_index<1>::type::const_iterator const_iterator;

unique_list() : container_() { }

void append_if_unique(const Value &element) { container_.template get<0>().insert(element); }

const_iterator begin() const { return container_.template get<1>().begin(); }

const_iterator end() const { return container_.template get<1>().end(); }

private:
container_type container_;
};


int main()
{
unique_list<int> ulist;

ulist.append_if_unique(2);
ulist.append_if_unique(1);
ulist.append_if_unique(2);
ulist.append_if_unique(3);
ulist.append_if_unique(1);
ulist.append_if_unique(-6);

// Should print 2, 1, 3, -6
std::copy(ulist.begin(), ulist.end(), std::ostream_iterator<int>(std::cout, "\n"));

return 0;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!