# Can anyone help in Vectors?

I have heard that using vectors in programming contests would be very helpful.
I searched in Internet but i did not get the proper basics.Can anyone please give me a c++ programme to find the mean of 10 numbers using vector?(in the most simple manner :))I mean to just to get the basics of vectors?
Thanks

7 Likes

I hope this may be of some helpā¦

4 Likes

@kunnal361:is it necessary to create the vector as global??
Wonderful explanation
Please support young programmers like me.I am just a beginnerā¦
Thanks buddy

6 Likes

no need to make it globalā¦no problem at all dudeā¦also if you would like to see a fully functional code using an array of vectors you can see this solutionā¦http://www.codechef.com/viewsolution/1890251
it is my submission of a problem in the long contestā¦it is used for the adjacency listā¦!!!
pls do feel free to ask nething that u may not understand in the codeā¦

4 Likes

size() returns how many elements are there in the vector!!!

1 Like

bool is a datatype which is like boolean in JAVA it stores 1 or 0 depending upon true or false!!!

bool var=(true or ne number except 0) or bool var=(false or 0)

1 Like

i have used an array of vectorsā¦that is why i mentioned the sizeā¦!!! you can create vector of vectors in the following way:-

vector< vector >a;

You have mentioned C++, then why only vectors, why not more containers that C++ support?

The best way to learn about a feature of a language is by writing code in your own way trying different things.

Containers in C++ are classified as two types:

``````Sequence Containers and Associative Containers
``````

Arrays, vectors, lists etc fall in the former category whereas sets, maps etc fall in the latter one.

Both the categories have their own flavor when it comes to programming.

There are a lot of functions that help access to elements, capacity etc of any container. So why asking for a simple example to calculate mean of 10 numbers? Rather write your own code a number of times until youāre comfortable with the basic functions on an STL container. Then you will learn a lot of things like handling iterators carefully etc.

Everyone is in a learning phase, if looked from some perspective.

You can take some program as a reference in order to learn all this. Like I did, when I saw @anton_lunyovās submission for FIRESC, and few more examples in GCJ this year. But ultimately it is your own practice that counts.

Note: You tend to ask for a doubt too early, which is not good. In the learning phase, fight with a problem again and again, a lot of times before lurking for help.

2 Likes

Can anyone please give me a c++
programme to find the mean of 10
numbers using vector?(in the most
simple manner :))

``````int main(){

const int n = 10; /* number of the elements (>0) */
vector<int> vv;
int tmp;
for(int i=0;i<n;i++)
{
cin>>tmp;
vv.push_back(tmp); /* one way to insert the element to the last position in the vector */
}
double mean = accumulate(vv.begin(), vv.end(), 0.0) / vv.size(); /* or you can write /n */
cout<<"Required mean = "<< mean <<endl;
return 0;
}
``````

Code at Ideone.

2 Likes

No, it is not necessary.

@Debanjan:What is accumulate?
How can you sort bro?we Can access using a[i] and all nah!!?

@bibinpreethy: accumulate simply accumulate values in the range. The default operation is being just to return the sum of all elements. See here

Sorting a vector in non-decreasing order can be achieved simply by using `sort(vector.begin(),vector.end())`

Yes, we can access vector elements using subscript (a[i], in your example).

If we want in Decending order sort(a.end(),a.begin()).nah!!?
For non-increasing sorting, you can simply write: `sort(a.rbegin(), a.rend());` or `sort(a.begin(), a.end(), greater<int>());` whichever you prefer