0
array<int,4> a1 = {1,2,3,4};

array<int,4>::iterator itr1 = a1.begin(); //Ok

array<int>::iterator itr2 = a1.begin(); //Compiler error. why not allowed?

//Iteration
    while(itr1 != a1.end())
    {
        cout<<"\n "<<*itr1;
        itr1++;
    }

since we always iterate from begin() to end() Is there any special reason for mentioning size in array iterator template?

2
  • Downvoters please comment. Commented Nov 12, 2013 at 20:07
  • 1
    What do you expect array<int> to be std::array takes two template params Commented Nov 12, 2013 at 20:13

2 Answers 2

2

When you use a template with different template arguments the generated classes are not in any sense the same. So std::array<int, 1> and std::array<int, 2 are not the same type. When you instantiate the template a specialised copy of the class is created thus the above example creates two distinct specialisations of std::array one with the arguments <int, 1> and one with <int, 2>. Even though the specialisations have very similar implementation they are not the same. As a consequence of this std::array<int, 1>::iterator is not the same type as std::array<int, 2>::iterator. They are two different specialisations.

Your attempt with std::array<int>, which seems pretty logical at first glance, is not correct because std::array expects two template arguments and not one. The auto keyword was introduced in part to make these kinds of things easier, use it.

PS: Thanks to aaronman for clarifying the answer.

Sign up to request clarification or add additional context in comments.

11 Comments

WTF, how does this even answer the question, your answer has nothing to do with iterators
I expanded on that. I probably got a little carried away with the answer, sorry about that.
Sorry this answer is still not right it shouldn't be accepted
@aaronman. In what way do you think it's wrong? I'd be glad to change it if you have a better or more clear wording than me.
@aaronman: Concur, no argument there. I just wanted to clarify that iterator type is implementation defined, as this comment seemed to suggest otherwise, and the edited answer makes claims about the implementation. It is subtle detail, but I wanted accentuate the difference, as I have seen it be the source of a problem in code.
|
2

std::array is a compile time fixed size container which has its advantages. (For a run time dynamic container you can use std::vector) The size must be set at compile time so, array<int> is illegal and even doesn't exists as a type.

This type is a good alternative for traditional arrays such as int a[123]. You can pass std::array to functions easier than traditional arrays and whenever you're using std::array you know the length.

Functions .begin() and .end() make it consistence to other containers and you can iterate over it like other STL types. It's important to know array<int, 10> and array<int, 11> are two different types with same interfaces.

2 Comments

This question is a disaster, I expect this when answering java questions but c++11, for shame!
@aaronman: Hahaaa, Poor java programmers, I hope they don't see your comment :-))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.