Dominique
Dominique

Reputation: 17493

How to replace std::vector by linked list?

I have used std::vector for making my algorithm. I would like to replace the vectors by linked lists.

In order to do so, I was thinking of using the std::list, but I have no idea how to do this, for example I have tried following example for finding a value within a vector/list:

void find_values_in_vector(const std::vector<int>& input_vector, int value, int &rv1, int &rv2)
{
  if (input_vector[0] >= value) { // too small
    rv1 = 0; rv2 = 0; return;
  }
  int index = (int)input_vector.size() - 1;
  if (input_vector[index] <= value) { // too big
    rv1 = index; rv2 = index; return;
  }
  // somewhere inside
  index = 0;
  while (input_vector[index] <= value) {
    index++;
  }
  rv1 = index - 1; rv2 = index; return;
}

void find_values_in_list(const std::list<int>& input_list, int value, int &rv1, int &rv2)
{
  if (*input_list.begin() >= value) { // too small
    rv1 = 0; rv2 = 0; return;
  }
  if (*input_list.end() <= value) { // too big
    rv1 = (int)input_list.size() - 1; rv2 = (int)input_list.size() - 1; return;
  }
  // somewhere inside
  int index = 0; int temp = *input_list.begin();
  while (temp <= value) {
    temp = *input_list.next(); index++;
  }
  rv1 = index - 1; rv2 = index; return;
}

This seems not to work, as the member function next() is not existing. However I remember that browsing through a linked list is done by going to the beginning, and moving further to the next element until the a certain point is reached. I have seen that there is a way to get this done by using an interator in a for-loop, but I wonder what's wrong with my approach? I was under the impression that a std::list was a standard implementation of a double-directional linked list, or am I wrong and in that case, what std class is the implementation of a linked list (it does not need to be a double-directional linked list)?

Upvotes: 0

Views: 621

Answers (2)

This should work for vector, list, deque, and set (assuming the contents are sorted).

template <class T>
void find_values_in_container(const T& container, int value, int &rv1, int &rv2)
{
  rv1 = rv2 = 0;  // Initialize
  if (container.empty() || container.front() >= value)
  {
    return;
  }
  for (const auto& v : container)
  {
     rv2++;
     if (v > value)
     {
         break;
     }
     rv1++;
  }
  return;
}

Upvotes: 1

Hayt
Hayt

Reputation: 5370

The standard way to iterate through containers is like this:

for(std::list<int>::iterator it = input_list.begin();
    it != input_list.end();
    it++)
{
    ....
}

This also works for vectors,maps,deque,etc. The Iterator concept is consistently implemented throughout the STL so it's best to get used to this concepts.

There are also iterator operations like std::distance and std::advance etc. for the different types of iterators (I suggest you read up on them and their advantages/limitations)

If you have C++ 11 available you can also use this syntax (may not be useful for your problem though.)

for(const auto& value : input_list)
{
   ...
}

This also works throughout the STL container.

Upvotes: 2

Related Questions