Skip to main content

C++ Vector : Basics Of Vector in C++ STL

In C++, Vector is unlike array, it stores different data types such as int, double, string, float, etc. It works like the dynamic array with the ability to resize automatically itself. Vector stores data in the contiguous memory location. Two functions are needed to traverse from starting to end that is begin() and end() functions.
                             

Syntax of the vector declaration:-

            vector<data_type> variable_name (number_of_elements); 

Here number_of_elements is optional, we can also declare a vector with empty vector that contains zero elements. The data_type in the angle-brackets indicates any type of data type which is valid in c++.

Vector declaration examples:-

vector<int> numbers (10);
//In this example, we declared a vector name number of 10 integers.
vector<string> names; 
//In this, a vector name declared of string.

In every program of the vector it is necessary to add the header file at the top of the program. We must provide the appropriate #include directive at the top, since vector is a Standard Library, not in build in part of a core language. We must specify the vector header file such as
 
      #include<vector>
After a vector has been declared specifying a certain number of elements, we can refer to individual elements in the vector using square brackets to provide a subscript or index, as shown below:-
   
        numbers[10];
Some algorithms are used in the Standard library such as sort(), reverse(), random_shuffle(), max_element(), min_element(),etc. So, it is necessary to add a header file that is #include<algorithm>.
For iterating the for loop or any task which star from the beginning of the vector to the end, vector uses the begin() function and end() function.

A simple example to illustrate the vector:
 
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
   //Declaration of vector container
  vector<int> numbers(5);
   cout<<"Enter the 5 values:-";
   
   for(vector<int>::iterator it = numbers.begin(); it! = numbers.end(); it++)
   {
       cin>>*it;
   }
   sort(numbers.begin(),numbers.end());
   cout<<"After sorting:-"
    for(vector<int>::iterator it = numbers.begin(); it! = numbers.end(); it++)
    {
       cout<<*it<<" ";
    }
   return 0; 
}

Output:-

Enter the 5 values:-
8
2
9
1
10
After sorting:-
1 2 8 9 10

This is the sorting of the 5 numbers, this program contains an iterator which is used to iterate the for loop from beginning of the vector to its end. In this header file are used such as #include<iostream>, #include<vector>, #include<algorithm>, these are necessary to execute all the code of the programs because the core language have not build in vector in it.

Comments

Popular posts from this blog

API : What is an Application Programming Interface

Application Programming Interface API का पूरा नाम "Application Programming Interface" हैं| यह एक software intermediary है जो दो software applications को एक दूसरे से बात करने का कम करता हैं| जैसे कि उद्धरण के लिए Instagram पर आपको message आता है और आप अपने phone पर check कर लेते है API के कारण, क्योंकि API एक माध्यम है आपके फ़ोन में आप किसी भी application से इनफार्मेशन प्राप्त कर पते  हैं| जब आप अपने phone में  कोई  application use करते है तो application internet से जुड़ता है और data को server पर  भेजता है| तब वह server, data को लेता है और translate करता है इस data को , उसके बाद उस data पर जरुरी कार्य करता है और अंतिम में जो translated data है उसे phone पर send करता हैं| तब Application उस data को translate करता है और आपके फ़ोन पर दिखता है readable form में जैसा आप देखना चाहते हैं|  ये जितने भी कार्य है, phone से data को लेना और उसे translate कर के server को send करना और फिर उसे phone पर user के readable form में change करके उसके फ़ोन पर डिस्प्ले करना, ये सब API के मा...

Fix Breadcrumbs Error And m=1? In Blogger

  Remove Breadcrumbs Errors And ?m=1 Breadcrumb is a small path. It points your location on your site. It also affects the SEO of your website.  Use canonical URL , so: data:blog.url.canonical  instead of data:blog.url. It should work for both desktop and mobile versions. It means the same URL works on both mobile and desktop without producing the ?m=1 or ?m=0. Before this, I am going to tell you what all the portions I will explain and fix your problem. First we will fix Breadcrumbs Error . This is the most dangerous for your website. Second we will remove ?m=1 and ?m=0, tags from our mobile view URL. Remove Breadcrumbs Error :- You should follow the below steps, so that you would fixed this error :- Go to blogger.com Login to blogger with your blogger email id Click on Template Click edit HTML, you have to edit HTML theme code. Press Ctr + F to open a search bar in your HTML theme code. Search for <div class='breadcrumbs' Paste the below code up to </b:loop> ...

Blogger : How To Create Blog Through Blogger & Its Overview

Overview Of Creation Of Blog Now days online learning and earning is very popular. Most of the people is trying to either learn or earn from their home. If you are a writer or story teller, you have interest in writing or reading new thing. So, This is very helpful for you, in this post i will tell you how to create your own blog website and earn money. Step follow to create a blog on blogger:- First you should have a email account and phone number to open or create a blogging account. You have to go to blogger website page, URL of that page is given below, simply you go and click on the URL. Click on the create new blog, after clicking, you have enter the blog name that you want to show to the user and you must choose or set the appropriate your own blog URL. After this process, you will have created your blog. You can handle everything post, pages, layout, inside the setting,you can set the user information and the account information. In the layout you can set layout from the given ...