-3

I am a beginner in c++ and I am trying to understand vectors.

I know the basic format which is:

vector <dataType> vectorName;

People are telling me that vectors are like arrays. But, what I don't

understand is that for arrays you can do this:

array[] = {1, 2, 3}

But for vectors you don't seem to get to set it to a list. Or do you have

to keep using .push_back().

Also, can you use something like vectorName[1] or not?

Can anyone explain this to me?

Thanks.

Ian
  • 687
  • 3
  • 8
  • 17
  • 1
    In Your free time, you should check [The Definitive C++ books and guide list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) out. Please select at least two from there.. – WhiZTiM Aug 18 '16 at 00:11
  • another thing, first welcome to stackoverflow - second, i think there would be a lot of penalty for such question, if not for us at least for you - it's a very standard question, that show no research effort. – Miro Rodozov Aug 18 '16 at 00:52
  • seems it is not asking about the usage of vector, but how to init a vector more easily only – ggrr Aug 18 '16 at 01:49

5 Answers5

5

You can use the style if you use C++11 or later.

#include <iostream>
#include <vector>

int main(void) {
    std::vector<int> vec = {1, 2, 3};
    std::cout << vec[1] << std::endl;
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
2

The whole purpose of vectors is to be "infinite", so you don't have to redefine it everytime you need to expand it.

push_back is made so you can add/expand to the array without redefining it; you still access and modify like a normal array:

std::vector<int> a;
a.push_back(2);
a.push_back(6);
std::cout << a[0] << std::end; //2
std::cout << a[1] << std::end; //6
a[0] = 5;
a[1] = 7;
std::cout << a[0] << std::end; //5
std::cout << a[1] << std::end; //7

You can also initialize it old-school style (the = is optional):

std::vector<int> a {2, 6};
std::cout << a[0] << std::end; //2
std::cout << a[1] << std::end; //6
jv110
  • 353
  • 2
  • 13
1

Try this for C++:

#include <iostream>
#include <vector>

int main(void) {
    std::vector<int> vec { 34,23 };
    return 0;
}

Or even:

#include <iostream>
#include <vector>

int main(void) {
    std::vector<int> v(2);
    v = { 34,23 };
    return 0;
}
Vaibhav Bajaj
  • 1,934
  • 16
  • 29
1

seems non of the above gave you any hint on dealing with the vector after you created it, so, let's say you created it with few initial values.

#include <iostream>
#include <vector>

int main(void) {
     std::vector<int> vec { 34,23 };
     // use push_back(some_value) if you have multiple values to put inside, say 1000. 
     // instead of using the index [] brackets, try .at() method 
     std::cout << vec.at(1) << std::endl;



     // use both the .size() or the new syntax to loop over it 
     for (unsigned i = 0 ; i < vec.size() ; i++){
       std::cout << vec.at(i) << std::endl;
     }
     // or the new syntax 
     for (auto & i : vec){
       std::cout << i << std::endl;
     }

     return 0;
}

Have fun :)

Miro Rodozov
  • 197
  • 1
  • 10
0

Vectors are expandable arrays. Unlike an array, you are not restricted to the size you initialized it with.

Growth of vector: the vector doubles it size whenever you overflow it. Underneath the hood its still an array but the "expandable" property of it comes by copying the contents of the previous array into a new larger array.

Few things that you can do with vectors in C++

Initialization of vector

vector<Type> one (size, defaultValue);
vector<Type> two (one); // makes a new vector *two* with the contents of *one*
vector<int> three {1,2,3}; // using initializer list

Accessing an element

int temp = three[2]; //array like syntax
int temp = three.at(2);

You cannot increase the size of a vector using this syntax i.e. you cannot dothree[3]=4;

Expanding

three.pusk_back(4);

Shrinking

three.pop_back();