Be a Supporter!

Stackoverflow 400 bounty

  • 212 Views
  • 1 Reply
New Topic Respond to this Topic
polym
polym
  • Member since: Oct. 2, 2007
  • Offline.
Forum Stats
Member
Level 14
Audiophile
Stackoverflow 400 bounty 2013-05-08 09:19:33 Reply

Hey, I posted an easy question on stackoverflow, and put a 400 reputation bounty on it. If any of you have stackoverflow accounts, feel free to head on over and add your input. Not only will you be helping the community, but you might have the possiblity to get 400 rep. Here's the link:

http://stackoverflow.com/questions/16380740/stdarray-vs-stdv ector-subtle-difference

Title of the question: std::array vs std::vector subtle difference

Just so you don't have to click on the link to read, here's the question in full:

I wanted to mess around with std::array to see how different it is from std::vector. So far, I've only found one major difference.

Sentence sentence = { "Hello", "from", "GCC", __VERSION__, "!" };  
std::array<std::string, 10> a;
std::copy(sentence.begin(), sentence.end(), a.begin());

int i = 0;
for (const auto& e : a)
{
    i++;
    std::cout << e << std::endl;
}
std::cout << i << std::endl;

// outputs 10

i = 0;
for (const auto& e : sentence)
{
    i++;
    std::cout << e << std::endl;
}
std::cout << i << std::endl;

// outputs 5

for (int i = 0; i < a.size(); i++)
    std::cout << i << " " << a[i] << std::endl;

// outputs 0 Hello
// ...
//         4 !
//         5-9 is blank

for (int i = 0; i < sentence.size(); i++)
    std::cout << i << " " << sentence[i] << std::endl;

// outputs 0 Hello
// ...
//         4 !
// stops here


// The following outputs the same as above
i = 0;

for (auto it = a.begin(); it != a.end(); it++)
{
    std::cout << i << " " << *it << std::endl;
    i++;
}
std::cout << i << std::endl;
i = 0;
for (auto it = sentence.begin(); it != sentence.end(); it++)
{
    std::cout << i << " " << *it << std::endl;   
    i++;
}
std::cout << i << std::endl;

So from what I can see, std::array's size and max_sizeis redundant, but std::vector's size and capacity can be different or the same. This is even confirmed from this quote:

size and max_size of an array object always match.

So why does std::array have redundant size functions? More importantly, would you consider it a gotcha that std::array's size is not necessarily the same as std::vector's size, because the vector has a capacity? Also, does this mean that std::arrays are safe (i.e., do they have smart pointer management like vectors?)

Tags: arrays c++11 vector

If you know basic C++, like I said, feel free to leave a comment or an answer.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Online!
Forum Stats
Moderator
Level 13
Programmer
Response to Stackoverflow 400 bounty 2013-05-08 10:22:46 Reply

At 5/8/13 09:19 AM, polym wrote: So from what I can see, std::array's size and max_sizeis redundant

I wouldn't call them redundant since it's useful to know the size of an std::array after it has already been constructed, but having both is a bit redundant since they're always equal and never change; they both exist to just keep compatibility with the rest of the STL.

At 5/8/13 09:19 AM, polym wrote: but std::vector's size and capacity can be different or the same.

It's pretty uncommon for a vector's size and capacity to be equal (though it is possible). The size of the vector is just the number of elements it contains; the capacity is the number of elements that can fit in the vector's current memory that has been allocated on the heap. A vector's capacity can never shrink and will increase exponentially as more elements are added (it doubles each time the capacity is exceeded).

At 5/8/13 09:19 AM, polym wrote: So why does std::array have redundant size functions?

As I said earlier in my post it's just to keep compatibility and consistency with the rest of the STL. Every container has those two functions: vector, list, queue, deque, map, et cetera.

At 5/8/13 09:19 AM, polym wrote: More importantly, would you consider it a gotcha that std::array's size is not necessarily the same as std::vector's size, because the vector has a capacity?

No. A vector has the ability to change its size whereas an array does not, so it makes perfect sense for them to have different sizes (and a vector's capacity cannot be compared to an array as an array has nothing similar to it).

At 5/8/13 09:19 AM, polym wrote: Also, does this mean that std::arrays are safe (i.e., do they have smart pointer management like vectors?)

You can store smart pointers in them, yes.
Just like with a vector I don't recommend storing raw pointers to dynamically allocated memory though; that is likely to result in a memory leak.