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?
Please help me out.I looked in cplusplus.com.Can anyone help me out?I am just a beginner. :frowning:
Thanks

7 Likes

I hope this may be of some help…:slight_smile:

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…:slight_smile:
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…:slight_smile:

@kunal361:link text there i have done made my doubts as comments.If possible please guide me dude…:slight_smile:

4 Likes

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 :))

How about using accumulate?

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

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)

@kunal361:what about the 4th line!!!

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;

10/10 answer. Perfect!!!

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!!?
Nice answer…:slight_smile:
Thnkz bro…:slight_smile:
Please Keep supporting beginners like me.
Happy Coding.Happy Day…:slight_smile:

For non-increasing sorting, you can simply write: sort(a.rbegin(), a.rend()); or sort(a.begin(), a.end(), greater<int>()); whichever you prefer :slight_smile: