(30). 19.3 Vector in CPP By College Wallah. - anishsingh90/Data_Structure_And_Algorithm_In_Cpp_github.io GitHub Wiki

//Basic code in vector to use basic syntex. #include <bits/stdc++.h> using namespace std;

int main(){ vector v;

cout << "Size: "<< v.size() << endl;
cout <<"Capacity: " << v.capacity() << endl;

v.push_back(1);
cout << "Size: "<< v.size() << endl;
cout <<"Capacity: " << v.capacity() << endl;


v.push_back(2);
cout << "Size: "<< v.size() << endl;
cout <<"Capacity: " << v.capacity() << endl;


v.push_back(3);
cout << "Size: "<< v.size() << endl;
cout <<"Capacity: " << v.capacity() << endl;

v.resize(5);
cout << "Size: "<< v.size() << endl;
cout <<"Capacity: " << v.capacity() << endl;

v.resize(10);
cout << "Size: "<< v.size() << endl;
cout <<"Capacity: " << v.capacity() << endl;

v.pop_back();
v.pop_back();


return 0;

}

//FOR LOOP IN VECTOR #include #include using namespace std;

int main(){ vector v; //for loop for(int i=0; i<5; i++){ int element; cin >> element; v.push_back(element); }

cout << "for loop: " << endl;
for(int i=0; i<v.size(); i++){
	cout << v[i] << " ";
}
cout << endl;

//for each loop cout << "for each loop: " << endl; for(int ele:v){ cout << ele << " "; } cout <<endl;

//while loop
cout << "while loop: " << endl;
int idx=0;
while(idx<v.size()){
    cout << v[idx++] << " ";
}


return 0;

}

/* OUTPUT: 1 2 3 4 5 for loop: 1 2 3 4 5 for each loop: 1 2 3 4 5 while loop: 1 2 3 4 5 */

//USE OF Insert And Erase Function Of Vector In For Loop. #include #include using namespace std;

int main(){ vector v; //for loop for(int i=0; i<5; i++){ int element; cin >> element; v.push_back(element); }

cout << "for loop: " << endl;
for(int i=0; i<v.size(); i++){
	cout << v[i] << " ";
}
cout << endl;

v.insert(v.begin()+2,6);

//for each loop cout << "for each loop: " << endl; for(int ele:v){ cout << ele << " "; } cout <<endl; v.erase(v.end()-2);

//while loop
cout << "while loop: " << endl;
int idx=0;
while(idx<v.size()){
    cout << v[idx++] << " ";
}


return 0;

} /* OUTPUT: 1 2 3 4 5 for loop: 1 2 3 4 5 for each loop: 1 2 6 3 4 5 while loop: 1 2 6 3 5 */

//Find The Last Occurance Of An Element X In A Given Array. #include #include using namespace std;

int main(){ vector v(6); cout << "Enter the element: " << endl; for(int i=0; i<6; i++){ cin >> v[i]; }

int x;
cout << "Enter x: ";
cin >> x;

int occurance=-1;
for(int i=0; i<v.size(); i++){
    if(v[i]==x){
       occurance =i;
       
    }
}

cout << occurance << endl;

return 0;

}

/* OUTPUT: Enter the element: 1 2 3 5 3 6 Enter x: 6 5 */

//Count The Number Of Occurance Of A Particular Element X. #include #include using namespace std;

int main(){ vector v(6); cout << "Enter the element: "; for(int i=0; i<v.size(); i++){ cin >> v[i]; }

int x;
cout << "Enter x: ";
cin >> x;

int occurances = 0;
for(int ele:v){
	if(ele==x){
		occurances++;
	}
}
cout <<occurances << endl;

return 0;

}

/* OUTPUT: Enter the element: 1 1 2 3 1 1 Enter x: 1 4 */

//Cont The Number Of Element Strictly Greater Than Value X. #include #include using namespace std;

int main(){ vector v(6); cout << "Enter the element: "; for(int i=0; i<v.size(); i++){ cin >> v[i]; }

int x;
cout << "Enter x: ";
cin >> x;


int count = 0;
for(int i=0; i<v.size(); i++){
    if(v[i]>x){
        count++;
    }
}
cout << count << endl;

return 0;

}

/* OUTPUT: Enter the element: 1 2 3 4 5 6 Enter x: 2 4 */

//Check If The Given Array Is Sorted Or Not. #include #include using namespace std;

int main(){ int array[] = {1,2,3,4,5,6};

bool sortedflag=true;
for(int i=1; i<6; i++){
    if(array[i] <= array[i-1]){
        sortedflag = false;
    }
}
cout << sortedflag << endl;

return 0;

}

/* OUTPUT: 1=true(array is sorted) */

//Find The Diffrence Between The Sum Of Element At Even-Odd; #include #include using namespace std;

int main(){ int array[] = {1,2,1,2,1,2};

int anssum=0; for(int i=0; i<6; i++){ if(i%2==0){ anssum += array[i]; } else{ anssum -=array[i]; } } cout <<"evensum-oddsum: "<<anssum << endl;

return 0;

}

/* OUTPUT: evensum-oddsum: -3 */

⚠️ **GitHub.com Fallback** ⚠️