creation of vector

May you explain this line?
std::vector<int> myvector (myints, myints+4);

From the example code here:
http://www.cplusplus.com/reference/algorithm/find/

From the code preceding this line, there is:
1
2
3
4
5
  int myints[] = { 10, 20, 30 ,40 };
  int * p;
  p = std::find (myints,myints+4,30);
// I see that here myints returns 0 and myints+4 returns 3 because pointer to *p is set to first element in the array.
// The find() applied on array searches elements 10,20,30 and returns iterator to 30 which is value 3. 


Now in the code
std::vector<int> myvector (myints, myints+4);
what exactly meant this convention in rounded brackets? I though, that the second parameter is used to initiate the elements to some value, but from this example this looks like it defines the range 0-4 which is 3 elements to search. Why not to type just
std::vector<int> myvector (myints+4);
or
std::vector<int> myvector (4);
to define the size of vector?
It's the "range" constructor. Its parameters are two iterators.
http://www.cplusplus.com/reference/vector/vector/vector/

1
2
3
template <class InputIterator>
         vector (InputIterator first, InputIterator last,
                 const allocator_type& alloc = allocator_type());


Other containers have "range" constructors as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
    // Message
    const std::string m("Hello, World!");

    // Reversed Message
    const std::string rm(m.rbegin(), m.rend());

    std::cout << rm << std::endl;
}
!dlroW ,olleH

Do yo also have examples to those next constructors? 2) fill constructor and 4รบ copy constructor.

Edit:
rm(iritator_begin, iritator_end)

I see now how you mean it to be!
std::string is the template/class
rm is the instance of vector and brackets are the constructor

this is missing the 3rd argument which should be returned so it does not keep the form of
1
2
3
template <class InputIterator>
         vector (InputIterator first, InputIterator last,
                 const allocator_type& alloc = allocator_type());


So you create an m instance of string class -> the constructor returns the string which is in input of the construct parameter.

Then you create an rm instance, but the construct accepts two parameters called parameter range (?), which iterates the string in reversed order, just like vectors (or arrays) can be iterated.

I found this is similar to Initialization of data members in a class
http://www.informit.com/articles/article.aspx?p=1852519
Last edited on
I am sorry, I do not understand what exactly you would like further explanations for.
Do you have more questions?
Thanks for your explanation. I think it is clear to me now.

According the link you pasted, there is 4 forms of constructor use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
default (1)	

explicit vector (const allocator_type& alloc = allocator_type());

fill (2)	

explicit vector (size_type n, const value_type& val = value_type(),
                 const allocator_type& alloc = allocator_type());

range (3)	

template <class InputIterator>
         vector (InputIterator first, InputIterator last,
                 const allocator_type& alloc = allocator_type());

copy (4)	

vector (const vector& x);


But there are examples on the page bellow:
1
2
3
4
5
  // constructors used in the same order as described above:
  std::vector<int> first;                                // empty vector of ints
  std::vector<int> second (4,100);                       // four ints with value 100
  std::vector<int> third (second.begin(),second.end());  // iterating through second
  std::vector<int> fourth (third);                       // a copy of third 


So it make sense now. Thanks.
Topic archived. No new replies allowed.