vectors

Jan 31, 2014 at 5:08pm
Can we compare vectors with linked lists?
Jan 31, 2014 at 5:21pm
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;
	}
}
Jan 31, 2014 at 5:22pm
Just with their characteristics.
Jan 31, 2014 at 5:27pm
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.
Jan 31, 2014 at 5:31pm
thanks!! @Peter87 and Disch.
Topic archived. No new replies allowed.