logo

Alusta Vector C++:ssa

Vektori voi tallentaa useita tietoarvoja, kuten taulukoita, mutta ne voivat tallentaa vain objektiviittauksia, eivät primitiivisiä tietotyyppejä. Ne tallentavat kohteen viittauksen tarkoittaa, että ne osoittavat objekteihin, jotka sisältävät tiedot, sen sijaan, että ne tallentaisivat niitä. Toisin kuin matriisi, vektoreita ei tarvitse alustaa koolla. Niillä on joustavuus mukauttaa kohdeviitteiden määrää, mikä on mahdollista, koska säiliö hoitaa niiden varastoinnin automaattisesti. Säilö säilyttää sisäisen kopion allocista, jota käytetään tallennustilan varaamiseen koko elinkaaren ajan. Vektorit voidaan paikantaa ja kulkea iteraattoreilla, joten ne sijoitetaan viereiseen varastoon. Vectorissa on myös turvaominaisuuksia, jotka säästävät ohjelmia kaatumiselta, toisin kuin Array. Voimme antaa varata tilaa vektorille, mutta emme taulukoille. Taulukko ei ole luokka, vaan vektori on luokka. Vektorissa elementtejä voidaan poistaa, mutta ei taulukoissa.

Ylätason 'Kokoelmaluokka' -vektori lähetetään malliluokan muodossa. Taulukko on alemman tason tietorakenne erityisominaisuuksineen. Vektoreilla on funktioita ja konstruktoreita; ne eivät ole indeksipohjaisia. Ne ovat vastakohta Arrayille, jotka ovat indeksipohjaisia ​​tietorakenteita. Tässä alin osoite annetaan ensimmäiselle elementille ja korkein osoite annetaan viimeiselle elementille. Vektoria käytetään objektin lisäämiseen ja poistamiseen, kun taas taulukoita käytetään usein objektien käyttöön. Taulukot ovat muistia säästäviä tietorakenteita, kun taas Vector käyttää paljon enemmän muistia vastineeksi tallennustilan hallintaan ja dynaamiseen kasvuun. Vector vie enemmän aikaa päästä käsiksi elementteihin, mutta näin ei ole taulukoiden tapauksessa.

On neljä tapaa alustaa a vektori C++:ssa :

  • Syöttämällä arvot yksitellen
  • Käyttämällä vektoriluokan ylikuormitettua konstruktoria
  • Taulukon avulla
  • Käyttämällä toista alustettua vektoria

Syöttämällä arvot yksitellen -

Kaikki vektorin elementit voidaan lisätä yksitellen käyttämällä vektoriluokkamenetelmää 'push_back'.

Algoritmi

 Begin Declare v of vector type. Then we call push_back() function. This is done to insert values into vector v. Then we print 'Vector elements: 
'. ' for (int a: v) print all the elements of variable a.' 

Koodi -

 #include #include using namespace std; int main() { vector vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5); vec.push_back(6); vec.push_back(7); vec.push_back(8); vec.push_back(9); vec.push_back(101); for (int i = 0; i <vec.size(); i++) { cout << vec[i] ' '; } return 0; < pre> <p> <strong>Output</strong> </p> <img src="//techcodeview.com/img/c-tutorial/62/initialize-vector-c.webp" alt="Initialize Vector in C++"> <h3>Using an overloaded constructor -</h3> <p>When a vector has multiple elements with the same values, then we use this method.</p> <p>By using an overloaded constructor of the vector class -</p> <p>This method is mainly used when a vector is filled with multiple elements with the same value.</p> <p> <strong>Algorithm</strong> </p> <pre> Begin First, we initialize a variable say &apos;s&apos;. Then we have to create a vector say &apos;v&apos; with size&apos;s&apos;. Then we initialize vector v1. Then initialize v2 by v1. Then we print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { int elements = 12; vector vec(elements, 8); for (int i = 0; i <vec.size(); i++) { cout << vec[i] ' 
'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 8 8 8 8 8 8 8 8 8 8 8 8 </pre> <h3>By the help of arrays -</h3> <p>We pass an array to the constructor of the vector class. The Array contains the elements which will fill the vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we create a vector say v. Then, we initialize the vector. In the end, print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vectr{9,8,7,6,5,4,3,2,1,0}; for (int i = 0; i <vectr.size(); i++) { cout << vectr[i] ' 
'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 9 8 7 6 5 4 3 2 1 0 </pre> <h3>Using another initialized vector -</h3> <p>Here, we have to pass the begin() and end() iterators of an initialized vector to a vector class constructor. Then we initialize a new vector and fill it with the old vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we have to create a vector v1. Then, we have to initialize vector v1 by an array. Then we initialize vector v2 by v1. We have to print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vec_1{1,2,3,4,5,6,7,8}; vector vec_2(vec_1.begin(), vec_1.end()); for (int i = 0; i <vec_2.size(); i++) { cout << vec_2[i] ' 
'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 5 6 7 8 </pre> <hr></vec_2.size();></pre></vectr.size();></pre></vec.size();></pre></vec.size();>

Koodi -

 #include #include using namespace std; int main() { int elements = 12; vector vec(elements, 8); for (int i = 0; i <vec.size(); i++) { cout << vec[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 8 8 8 8 8 8 8 8 8 8 8 8 </pre> <h3>By the help of arrays -</h3> <p>We pass an array to the constructor of the vector class. The Array contains the elements which will fill the vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we create a vector say v. Then, we initialize the vector. In the end, print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vectr{9,8,7,6,5,4,3,2,1,0}; for (int i = 0; i <vectr.size(); i++) { cout << vectr[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 9 8 7 6 5 4 3 2 1 0 </pre> <h3>Using another initialized vector -</h3> <p>Here, we have to pass the begin() and end() iterators of an initialized vector to a vector class constructor. Then we initialize a new vector and fill it with the old vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we have to create a vector v1. Then, we have to initialize vector v1 by an array. Then we initialize vector v2 by v1. We have to print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vec_1{1,2,3,4,5,6,7,8}; vector vec_2(vec_1.begin(), vec_1.end()); for (int i = 0; i <vec_2.size(); i++) { cout << vec_2[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 5 6 7 8 </pre> <hr></vec_2.size();></pre></vectr.size();></pre></vec.size();>

Taulukon avulla -

Välitämme taulukon vektoriluokan konstruktorille. Array sisältää elementit, jotka täyttävät vektorin.

Algoritmi -

 Begin First, we create a vector say v. Then, we initialize the vector. In the end, print the elements. End. 

Koodi -

 #include #include using namespace std; int main() { vector vectr{9,8,7,6,5,4,3,2,1,0}; for (int i = 0; i <vectr.size(); i++) { cout << vectr[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 9 8 7 6 5 4 3 2 1 0 </pre> <h3>Using another initialized vector -</h3> <p>Here, we have to pass the begin() and end() iterators of an initialized vector to a vector class constructor. Then we initialize a new vector and fill it with the old vector.</p> <p> <strong>Algorithm -</strong> </p> <pre> Begin First, we have to create a vector v1. Then, we have to initialize vector v1 by an array. Then we initialize vector v2 by v1. We have to print the elements. End. </pre> <p> <strong>Code -</strong> </p> <pre> #include #include using namespace std; int main() { vector vec_1{1,2,3,4,5,6,7,8}; vector vec_2(vec_1.begin(), vec_1.end()); for (int i = 0; i <vec_2.size(); i++) { cout << vec_2[i] \' 
\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 5 6 7 8 </pre> <hr></vec_2.size();></pre></vectr.size();>

Toisen alustetun vektorin käyttäminen -

Tässä meidän on välitettävä alustetun vektorin begin()- ja end()-iteraattorit vektoriluokan konstruktorille. Sitten alustamme uuden vektorin ja täytämme sen vanhalla vektorilla.

Algoritmi -

 Begin First, we have to create a vector v1. Then, we have to initialize vector v1 by an array. Then we initialize vector v2 by v1. We have to print the elements. End. 

Koodi -

 #include #include using namespace std; int main() { vector vec_1{1,2,3,4,5,6,7,8}; vector vec_2(vec_1.begin(), vec_1.end()); for (int i = 0; i <vec_2.size(); i++) { cout << vec_2[i] \\' 
\\'; } return 0; < pre> <p> <strong>Output</strong> </p> <pre> 1 2 3 4 5 6 7 8 </pre> <hr></vec_2.size();>