vectors

Can we compare vectors with linked lists?
Yes, of course. How exactly do you want to compare them? Equal elements in equal order?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <list>
#include <vector>
#include <algorithm>
#include <iostream>

int main(int argc, char** argv)
{
	std::vector<int> vector;
	std::list<int> list;

	if (vector.size() == list.size() && std::equal(vector.begin(), vector.end(), list.begin()))
	{
		std::cout << "Equal!" << std::endl;
	}
	else
	{
		std::cout << "Not equal!" << std::endl;
	}
}
Just with their characteristics.
Vectors:

- store elements congruently
- fast random access
- inserting/removing elements in the middle of the sequence requires shifting other elements over in order to keep them congruent in memory
- increasing size of the vector may require all elements be moved if more memory needs to be allocated



Lists:

- elements stored as individual nodes (not congruent)
- no random access, only sequential access
- inserting/removing elements in the middle of the sequence is fast. Does not require any other elements be moved.
- increasing size of the list will never require any elements to be moved.




Vectors are usually the better option. But it depends on what you're doing with the container.
thanks!! @Peter87 and Disch.
Topic archived. No new replies allowed.