Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views5 pages

Printf Vector

The document provides a comprehensive overview of various operations and functionalities related to vectors, strings, and sets in C++. It includes methods for accessing, modifying, and managing elements in vectors and strings, as well as unique characteristics of sets. Key operations such as insertion, deletion, sorting, and comparison are highlighted with examples.

Uploaded by

Lutetium Emon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Printf Vector

The document provides a comprehensive overview of various operations and functionalities related to vectors, strings, and sets in C++. It includes methods for accessing, modifying, and managing elements in vectors and strings, as well as unique characteristics of sets. Key operations such as insertion, deletion, sorting, and comparison are highlighted with examples.

Uploaded by

Lutetium Emon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Vector:

Printf vector: cout<<v[0]; , cout<<v.at(0);


if out of range v[x] printf 0
but v.at(x) will give error. Say out of range
-------------------
front()-- access 1st value. Cout<<v.front();
----------
back()-- same
--------------
clear()-- delete all value. Can check by v.size() give 0 value
--------
empty()-- check vector is empty or not.
-------
pop_back()-- remove last element
-----\
erase()-- delete any position element
v.erase(v.begin()+1) second element will delete
v.erase(v.begin()+1, v.end()) 2nd element to last element all will delete.
-------
insert()-- insert in any position
v.insert(v.begin()+1, 5) 5 will insert in 2nd pos.
v.insert(v.begin()+1,3, 5) 5 will insert from 2nd pos 3 times.
-------
swap()-- swap all element bet 2 vector.
Vector<int>v1={3,5,7,8};
Vector<int>v2={7,4,2};
swap(v1,v2);
v1={7,4,2}
v2={3,5,7,8}
--------
sort()-- sort(v.begin(), v.end())
sort(v.begin(), v.end(), greater<int>())
-----
reverse()-- reverse(v.begin(), v.end())
-----
resize()-- v.resize(10) fix to 10 size
-----
vector<int>v={4,6,1,4,6}
vector<int>vt
vt=v-- all element copy from v to vt
---------------
unique()-- return number of unique number
unique(v.begin(), v.end)-v.begin()
unique(v.begin(), v.begin()+4)-v.begin() 4 no index er aag porjonto unique
** need to sort first
**not v.unique() only unique()
------

String
string s;
s+=’c’;
-------
string s=”Emon”
s+=’c’>> Emonc
s+=s?? EmoncEmonc
cout<<s[0]; >> E
-----
s.clear()-- clear string
----
string s1=”uyu”, s2=”cwyb”;
s1=s2>> s1=”cwyb”
-----
s.empty()-- check empty ot not
-----
Iterator use
string s=”hjb iybu iy”;
string::iterator it;
for( it=s.begin(); it<s.end(); it++) cout<< *it;
or
for(auto u:s) cout<<u;
----
string compare
s1==s2
---
reverse()
reverse(s.begin(), s.end())-- reverse string
-----
scan string with space separeted
getline(cin,s);
for multiple line string scan:

-----------------------------
sort():
sort(s.begin(), s.end());
sort(s.rbegin(), s.rend());
----
unique():
first sort.
Int size=unique(s.begin(), s.end())-s.begin();
-----
max-min Element:
*max_element(s.begin(), s.end());
*min_element(s.begin(), s.end());
-------
erase():
s.erase(s.begin()+3); from begining
s.erase(s.end()-1);// s.pop_back() from last
*remove specific character:
s.erase(remove(s.begin(), s.end(), “A”), s.end());
-----
s.pop_back(): delete last element
s.back(): printf last element
-----
string of Vactor;
vector<string>v,
v.push_back(“Emon”);
v.push_back(“Enhbgv”);
can be sort:
sort(s.begin(), s.end()); wil sort lexograpgically
-----
compare string:
string s=”emon”;
string t=”eemon”;
this will compare lexograpgically
t will b smaller,
-----
int to string:
int a=123;
string s=to_string(a);
-----
string to int:
string s=”123”;
int a=stoi(s);
-----
isupper():
check a character is upper case or lowercase
char c=”A”;
bool c1=isupper(c);
cout<< c1; will print 0/1;
------

Copy():
String s=”emon”;
String t=”mnb mia nddw”;
Copy(t.begin()+4, t.begin()+7, back_insert(s));
Will print “emon mia”;
Find(): find for substring
String s=”emonone”;
If(s.find(“mon”)!=-1) printf(“yes”);
Else printf(“no”);

---------------------------------------------------------------

SET

Only unique variable in sorted.


Ar[]={1,2,1,2,4,1,3,3}
set={1,2,3,4}
element access, push … all log(N);
set<int>s;
* s.insert(2); // NO push_back()
* s.count(2); // if 2 is present in set>>1 or 0;
* cout<<*s.begin(); //1st element
* cout<<*(--s.end() ); or *rbegin(); //print last element
* s.erase(2); // will delete 2
* s.erase(s.begin() ); // 1st element delete
* s.erase(--s.end() ); or s.erase(s.rbegin() ) // have to use “—” to delete last
element
** set<pair<int, int>>s; // same as map but only unique element, automatically
sorted.
** usually sorted in increasing order.
For decreasing order>>
set<int, greater<int>>s;
for pair>>
set<pair<int, int >, greater<pair<int, int>>>pa;

*** unordered_set: will remain unordered but unique.


unordered_set<int>s={1,2,3}; //will may print 2 1 3;
normal complexity O(1). Worst case complexity O(n);

You might also like