https://github.com/imabhinavdev/cpp
https://github.com/imabhinavdev/cpp
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/imabhinavdev/cpp
- Owner: imabhinavdev
- Created: 2021-12-20T14:40:22.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-03-21T07:09:53.000Z (about 2 years ago)
- Last Synced: 2025-05-20T18:58:16.241Z (about 1 year ago)
- Language: C++
- Size: 121 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Containers
## Pairs
```cpp
pair p;
p=make_pair(3,"abc");
p={4,"cde"};
cout< p[3];
p[0]={1,4};
```
```cpp
pair p[3];
swap(p[0],p[1]);
cin>>p.first;
cin>>p.second;
```
## Vectors
Dynamic Array
* Vector Push
* Vector Pop
* Vector Size
* Vector Reference
```cpp
vector v;
v.push_back(); //O(1)
v.pop_back();
cout< v1(10); //all values will be v1
vector v2(10,3); //all valuse will be 3
vector &v3=v2 //passing reference
vector v2=v1 //O(n) we can use assignment operator here;
vector s;
```
* Nested Vector
```cpp
vector>v;
v.push_back({1,2});
v.push_back(make_pair(1,2));
vector> nestV;
```
### Iterators
* .begin()
* .end()
> it++ this moves to the next **Iterator** but it+1 moves it to the next **Location**. It it+1 cannot work in map and sets and locations are not continous.
```cpp
vector v;
v.push_back(10);
vector :: iterator it=v.begin()
cout<<*it<> vp;
vector>::iterator it1;
cout<<(*it1.first);
cout<<(it1->first);
cout<<(it1->second);
//it1->first == *it1.first
```
#### Iterator using C++ 11 | Auto Keyword
* Range Based Loops
```cpp
vectorv;
for(int val:v)
{
value++;//value is copied not referenced
cout<>vp;
for(pair &val:vp)
{
cout<>vp;
for(auto &val:vp)
{
cout< m;
m[1]="abc"; //O(logn)
m[5]="cde";
m[2]="efg";
m.insert({4,"afg"});
map::iterator it;
for(auto &val:m)
{
cout<Insertion of keys of string is **string.size()\*log(n)**
#### Unordered Maps
`Unorderd Map cannot work on pairs!`
>Use Hash Tables in this.
>O(1) is time complexity
Every method is taking O(1) like find(),erase()
```cpp
unordered_map m;
```
## Sets
1. Sets
2. Unordered Sets
3. Multiset
```cpp
set s;
s.insert("avc"); //log(n)
s.insert("def");
s.insert("ghi");
auto it=s.find("abc")//O(log(n)) returns iterator else returns s.end()
s.erase("avc");//removes the record.
```
#### Multiset
```cpp
multiset s;
s.find(); //returns the iterator of first match.
s.erase('abc');// delete all values abc or else if we pass the iterator then it will delete only one
```
## Stacks
```cpp
stack a;
a.push(3);
a.push(4);
a.push(5);
a.pop();
```
## Queues
```cpp
queue q;
q.push(4);
q.push(5);
q.empty();
q.front(); //prints the front value
q.pop();
```
### Sorting
```cpp
sort(starting address, last address+1); //O(nlog(n))
```
``Follow Intro Sort is used in STL.``
## Upper Bound and Lower Bound
Arrays or vectors should be sorted.
#### Lower Bound
If the value is present in array or vector than it returns that value and if not present then it returns the value greater then the input that is present in the array.
#### Upper Bound
Returns the value more than the input value in the array.
```cpp
int arr[5];
int *ptr=lower_bound(a,a+n,5);//returns the pointer
int *ptr1=lower_bound(a,a+n,5);//returns the pointer of element greater than 5.
```
### Minimum and Maximum Element
```cpp
vector v;
int min=*min_element(v.begin(),v.end());//returns pointer.
int max=*max_element(v.begin(),v.end());//returns pointer.
```
### Accumulate
Sums the array.
```cpp
vectorv;
int sum=accumulate(v.begin(),v.end(),initialSum);//initial sum should be 0 for arrray sum
```
### Count and Find, Reverse
Counts the element count.
```cpp
vector v;
int ct=count(v.begin(),v.end(),3);
int ele=*find(v.begin(),v.end(),2);//returns iterator
auto ele=find(v.begin(),v.end(),2);//returns iterator
if(ele !=v.end())
{
cout<<*it;
}
reverse(v.begin(),v.end()); //reverse vector or string
```
### Lambda Function
```cpp
cout<<[](int x,int y){return x+y;}(2,4);
```
### All of, None Of, Any of
All of
```cpp
vector v;
cout<0;}); //if all true then true else false
cout<0;}); //if any true then true else false
cout<0;}); //if none are true then true else false
```