Algorithim
Algorithim
#include <algorithm>
Here,
int main() {
return 0;
}
Run Code
Output
1 2 3 4 5
Here, we have set the sort() range from the beginning of our vector
using vec.begin() to the end of the vector using vec.end() .
This sorts our vector in ascending order.
Here,
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
Output
1 2 3 4 5
Here, we have created the source with five elements and an empty
vector destination of size 5.
We then used the copy() function to copy the contents
of source to destination .
For example,
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
return 0;
}
Output
Before move:
source: apple banana cherry
destination:
After move:
source:
destination: apple banana cherry
swap(x, y);
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
return 0;
}
Run Code
Output
Before swap:
vec1: 1 2 3
vec2: 4 5 6
After swap:
vec1: 4 5 6
vec2: 1 2 3
Here,
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> vec1 = {1, 3, 5};
vector<int> vec2 = {2, 4, 6};
vector<int> result(6);
return 0;
}
Run Code
Output
Before merge:
vec1: 1 3 5
vec2: 2 4 6
After merge:
result: 1 2 3 4 5 6
Here,
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
// replace 2 with 99
replace(vec.begin(), vec.end(), 2, 99);
return 0;
}
Run Code
Output
Before: 4 2 3 2 5
After: 4 99 3 99 5
Here,
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
return 0;
}
Run Code
Output
Before deletion: 4 2 3 2 5
After deletion: 4 3 5 2 5