JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

VECTOR CLASS FUNCTION in hindi , what is VECTOR CLASS FUNCTION in c++ language

इससे पहले के article मे , vector class के कुछ ही functions को discuss करेगे | अब इस article मे , vector class के कुछ advance function को discuss करेगे |

Capacity

इस function से किसी vector के capacity के साथ deal करता है | इसके लिए निन्म functions को use किया जाता है |

size() – इस function का use किसी vector के द्वारा hold किये गये  values के number को return करता है |

max_size() – इस function का use किसी vector के द्वारा hold की गयी  values की maximum number को return किया जाता है |

capacity() – इस function का use किसी vector के elements के द्वारा allocate storage space को return करता है | इसके लिए  number of element को single element के storage space को multiply किया जाता है

resize() – इस function का use, किसी container को resize करने के लिए किया जाता है जो की elements ‘g’ contain करता है |

empty() – इस function का use status को return करने के लिए किया जाता है जो की container की emptyness के status को check करता है |

shrink_to_fit() – इस function का use किसी container की capacity को reduce किया जाता है जो की किसी container की size को fit किया जाता है और अगर किसी container की capacity , selected size से ज्यादा होता है तब extra element कू destroy किया जाता है |

नीचे दिए गये प्रोग्राम मे , c++ प्रोग्राम मे  capacity के functions को use किया जाता है :-

// C++ program to illustrate the

// capacity function in vector

#include <iostream>

#include <vector>

using namespace std;

void main()

{

vector<int> v1;

for (int i = 1; i <= 5; i++)

v1.push_back(i);

cout << “Size : ” << v1.size();

cout << “\nCapacity : ” << v1.capacity();

cout << “\nMax_Size : ” << v1.max_size();

// resizes the vector size to 4

v1.resize(4);

// prints the vector size after resize()

cout << “\nSize : ” << v1.size();

// checks if the vector is empty or not

if (v1.empty() == false)

cout << “\nVector is not empty”;

else

cout << “\nVector is empty”;

// Shrinks the vector

v1.shrink_to_fit();

cout << “\nVector elements are: “;

for (auto it = v1.begin(); it != v1.end(); it++)

cout << *it << ” “;

getch();

}

Output:

Size : 5

Capacity : 8

Max_Size : 4611686018427387903

Size : 4

Vector is not empty

Vector elements are: 1 2 3 4

Element access:

reference operator [g] – इस function का use,किसी vector के element के address को point out करता है |

at(g) –  इस function का use,किसी vector के element के address / reference को point out करता है |

front() – इस function का use,किसी vector के first element के address / reference को point out करता है |

back() –  इस function का use,किसी vector के last element के address / reference को point out करता है |

data() – इस function का use , vector के array के डायरेक्ट pointer को return करता है | इसमें vector के data element को hold किया जाता है |

इस उदहारण मे , किसी vector के element के मेमोरी acess से संबंदित function को discuss किया है :-

// C++ program to illustrate the

// element accesser in vector

#include <bits/stdc++.h>

using namespace std;

int main()

{

vector<int> v1;

for (int i = 1; i <= 10; i++)

v1.push_back(i * 10);

cout << “\nReference operator [g] : v1[2] = ” << v1[2];

cout << “\nat : v1.at(4) = ” << v1.at(4);

cout << “\nfront() : v1.front() = ” << v1.front();

cout << “\nback() : v1.back() = ” << v1.back();

// pointer to the first element

int* pos = v1.data();

cout << “\nThe first element is ” << *pos;

return 0;

}

Output:

Reference operator [g] : v1[2] = 30

at : v1.at(4) = 50

front() : v1.front() = 10

back() : v1.back() = 100

The first element is 10

Modifiers:

इसमें vector class के Modifiers को discuss करेगे |

assign() – इस function का use किसी नए element को किसी vector के पुराने element को replace किया जाता है |

push_back() – इस function का use , किसी vector मे element को push किया जाता है pushing की direction backward होती है |

pop_back() – इस function का use , किसी vector मे element को pop / remove किया जाता है pushing की direction backward होती है |

insert() – इस function का use किसी vector मे किसी specified position से पहले data item को insert किया जाता है |

erase() – इस function का use किसी vector मे किसी specified position से data item को delete किया जाता है |

swap() –  इस function का use किसी vector मे data elements को swap करने के लिए किया जाता है | लेकिन दोनों vector की size same होनी चाहिए |

clear() – इस function का use किसी container के सभी data elements को डिलीट करने के लिए किया जाता है |

emplace() – इस function का use किसी container की size को बढाया जाता है |

// C++ program to illustrate the

// Modifiers in vector

#include <bits/stdc++.h>

#include <vector>

using namespace std;

void main()

{

// Assign vector

vector<int> v;

// fill the array with 10 five times

vec.assign(5, 10);

cout << “The vector elements are: “;

for (int i = 0; i < vec.size(); i++)

cout << vec[i] << ” “;

// inserts 15 to the last position

vec.push_back(15);

int n = vec.size();

cout << “\nThe last element is: ” << vec[n – 1];

// removes last element

vec.pop_back();

// prints the vector

cout << “\nThe vector elements are: “;

for (int i = 0; i < vec.size(); i++)

cout << vec[i] << ” “;

// inserts 5 at the beginning

vec.insert(vec.begin(), 5);

cout << “\nThe first element is: ” << vec[0];

// removes the first element

vec.erase(vec.begin());

cout << “\nThe first element is: ” << vec[0];

// inserts at the beginning

vec.emplace(vec.begin(), 5);

cout << “\nThe first element is: ” << vec[0];

// Inserts 20 at the end

vec.emplace_back(20);

n = vec.size();

cout << “\nThe last element is: ” << vec[n – 1];

// erases the vector

vec.clear();

cout << “\nVector size after erase(): ” << vec.size();

// two vector to perform swap

vector<int> v1, v2;

v1.push_back(1);

v1.push_back(2);

v2.push_back(3);

v2.push_back(4);

cout << “\n\nVector 1: “;

for (int i = 0; i < v1.size(); i++)

cout << v1[i] << ” “;

cout << “\nVector 2: “;

for (int i = 0; i < v2.size(); i++)

cout << v2[i] << ” “;

// Swaps v1 and v2

v1.swap(v2);

cout << “\nAfter Swap \nVector 1: “;

for (int i = 0; i < v1.size(); i++)

cout << v1[i] << ” “;

cout << “\nVector 2: “;

for (int i = 0; i < v2.size(); i++)

cout << v2[i] << ” “;

}

Output:

The vector elements are: 10 10 10 10 10

The last element is: 15

The vector elements are: 10 10 10 10 10

The first element is: 5

The first element is: 10

The first element is: 5

The last element is: 20

Vector size after erase(): 0

Vector 1: 1 2

Vector 2: 3 4

After Swap

Vector 1: 3 4

Vector 2: 1 2

इस उदाहरन मे vector के सभी operation को discuss किया गया है |

Sbistudy

Recent Posts

सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है

सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…

10 hours ago

मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the

marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…

10 hours ago

राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi

sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…

2 days ago

गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi

gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…

2 days ago

Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन

वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…

3 months ago

polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten

get all types and chapters polity notes pdf in hindi for upsc , SSC ,…

3 months ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now