STL & Data Structures

Hello my dear fellow programmers. My name is Aleksandar, I'm 24 years old and I've been "programming" in C++ for couple of months now, started with C at my university and then moved to C++.

Lately, I've been reading a lot about STL (Vectors, Maps, Lists) and other data types like structures, enumerations and stuff but my problem is that I can't get a reference for where to use certain data structure.

For my level of knowledge and experience, I don't find vectors any more useful then arrays, since for my very few console project I write, both are doing the same thing.

My question is, where can I find a good reference (or buy a book) that covers and provides examples and force you to do certain exercises.

I'm also really looking for some Book that involves assignments and stuff to do cause now I m reading about a lot of stuff and I have no clue where to use it...

Thanks in advance,
Aleksandar
http://stackoverflow.com/q/388242/1959975
Look at Effective STL

C++ is a very different beast from C. The way you program in C completely contradicts the way you program in C++.
Last edited on
http://www.amazon.com/Effective-STL-Specific-Standard-Template/dp/0201749629

by the way what you're experiencing is completely normal, we all didn't know which data structure is the best for certain tasks at first, but as you're learning more and more C++ you'll start to understand which is the most suitable data to use for certain tasks.

Vectors have certain advantages over arrays, think of a vector as a dynamic array - an array that can grow and shrink in size as needed, i almost always use vectors instead of arrays, vectors can also be used with the STL algorithms while arrays cannot, which means you're gonna have to write the algorithms yourself (such as: seach, sort etc..) if you're working with arrays. So you can see how vectors saves you a lot of time, however there are certain disadvantages when compared to arrays, vectors require a bit of extra memory as overhead, and there also can be a performance cost when a vector grows in size, thought there is a member function that you can use to avoid this issue .reserve()
Last edited on
Thanks friend.
Yes, I m aware of that and I haven't spent too much time on C since I wanted OOP language, so basically I started with C++ besides my webDev experience and PHP/asp.net. I was actually looking to buy this book but I wasn't sure even tho I've seen all the amazing comments on the amazon. I also have Primer a bit older version so thinking to buy new Primer as well and the latest book released by Bjarne.

@UK Marine - Thanks for the positive answer, it really encourage me to push forward. I ll surely listen to your advice and I wish I joined these forums earlier, you guys are simply amazing. Thanks a ton! :)
Last edited on
UK Marine wrote:
vectors can also be used with the STL algorithms while arrays cannot, which means you're gonna have to write the algorithms yourself...


This is not true and arrays can be used with stl algorithms. You can provide pointers as arguements as opposed to iterators.

Vectors are recommended over arrays to avoid having user handling memory management by themselves seeing as vectors do this for you as @Uk Marine mentioned. Another stl container I don't see many people using is deque. This does the same thing as vector including similar methods. But I will recommend deque only for fast inserting and removal of elements at either ends of the container
Last edited on
As far as I've seen from one book, deque is similar to vector but vector has specified .begin() and the end itself is specified outside the vector, after the last element of the vector I mean.
decque allows for efficient insertion on both ends (vector can efficiently insert only elements at end), but do not compatible with c-arrays. (for example you can pass pointer to the first element of the vector to the qsort() function and it is guaranteed to work correctly, yo cannot do that with decque). It is actually specified .begin() and .end() too. As each STL container it is useful in certain situations. Often it is underlooked: I have seen peole which used list only because it has fast insertion at the beginning.

I generally avoid c-arrays, unless they are stack allocated or fixed size class member ones, but even in those situation I prefer std::array. Only exception: fixed-size char arrays as a buffer to read from binary file.

vectors are cool: they manages memory for you, prevents leaks, have almost none memory (+ 2 long long integers in my compiler) and CPU (subscript operator for vectors as fast as simple array access) overhead. Plus they have neat functions, knows their size and compatible with interfaces which expects c arrays.

.end() pointing past-the-end is a standard library requirement. It is like working with c arrays:
1
2
3
4
5
6
7
8
9
10
11
12
13
//vector
const int size = 10;
std::vector<int> x(size);
for(auto it = x.begin(); x != x.end(); ++it)
    //...

//Equivalent c-array
const int size = 10;
int* x = new int[size];
int* begin = x;
int* end = x + size;
for(auto it = begin; it != end; ++it)
    //... 
Last edited on
@MiNiPaa

If the array was allocated on the stack, then the same operations done on the vector in your example would be similar to the ones done for the array.
ex: http://ideone.com/yid9A5

For those who don't understand stack and heap terminology:
http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap
Just pretend that size is non-constant and actually is received from user.
I made a point that iterator operations and pointer operations in array are pretty similar. That is why I created variables begin and end in array example (I should have probably create them in vector example too to show similarity).
It was not an example to prove that vector is easier to use and less verbose.
Last edited on
Topic archived. No new replies allowed.