I have a vector with some values (3, 3, 6, 4, 9, 6, 1, 4, 6, 6, 7, 3), and I want to replace each 3 with a 54 or each 6 with a 1, for example and so on.
So I need to go through the vector first, get the [i] value, search and replace each 3 with a 54, but still keep relevant positions.std::set
is vector::swap a good way? I am not even sure how to begin this :(
I can't use push_back as that would not keep the correct order of values as that is important.
Please keep it simple; I am just a beginner :)
The tool for the job is std::replace:
std::vector<int> vec { 3, 3, 6, /* ... */ };
std::replace(vec.begin(), vec.end(), 3, 54); // replaces in-place
See it in action.
You can use the replace or replace_if algorithm.
Online Sample:
#include<vector>
#include<algorithm>
#include<iostream>
#include<iterator>
using namespace std;
class ReplaceFunc
{
int mNumComp;
public:
ReplaceFunc(int i):mNumComp(i){}
bool operator()(int i)
{
return i==mNumComp;
}
};
int main()
{
int arr[] = {3, 3, 6, 4, 9, 6, 1, 4, 6, 6, 7, 3};
std::vector<int> vec(arr,arr + sizeof(arr)/sizeof(arr[0]));
cout << "Before\n";
copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "\n"));
std::replace_if(vec.begin(), vec.end(), ReplaceFunc(3), 54);
cout << "After\n";
copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "\n"));
return 0;
}
You could loop through each element of the list.
std::vector<int> vec{3, 3, 6, 4, 9, 6, 1, 4, 6, 6, 7, 3};
for(int n=0;n<vec.size();n++)
if(vec[n]==3)
vec[n]=54;
Use the STL algorithm for_each. It is not required to loop, and you can do it in one shot with a function object as shown below.
http://en.cppreference.com/w/cpp/algorithm/for_each
Example:
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
void myfunction(int & i)
{
if (i==3)
i=54;
if (i==6)
i=1;
}
int main()
{
vector<int> v;
v.push_back(3);
v.push_back(3);
v.push_back(33);
v.push_back(6);
v.push_back(6);
v.push_back(66);
v.push_back(77);
ostream_iterator<int> printit(cout, " ");
cout << "Before replacing" << endl;
copy(v.begin(), v.end(), printit);
for_each(v.begin(), v.end(), myfunction)
;
cout << endl;
cout << "After replacing" << endl;
copy(v.begin(), v.end(), printit);
cout << endl;
}
Output:
Before replacing
3 3 33 6 6 66 77
After replacing
54 54 33 1 1 66 77
Related
I am trying to find unique element from the array these is question
Input : arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}
Output : arr[] = {1, 2, 3, 4, 5}
They give me correct output but why they give 0 at the end in output:
these is my output:
{1,2,3,4,5,0}
Code:
#include<iostream>
using namespace std;
int main(){
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
int n=sizeof(arr)/sizeof(arr[0]);
int c=0;
for(int j=0;j<=n;j++){
if(arr[j]!=arr[j+1]){
cout<<arr[j];
}
}
}
Except for std::cout, you code is much more C than ++.
std::unique of the C++ Standard Library does exactly what you want. There is no need to re-implement this.
Next there is the erase-remove idiom to delete the superfluous elements.
For the output, you can use std::for_each() or at least a range-based for loop.
And also, you don't want using namespace std;
A more modern solution looks like this:
#include <iostream>
#include <algorithm>
#include <vector>
int main(){
std::vector<int> arr {1, 2, 2, 3, 4, 4, 4, 5, 5};
auto last = std::unique(arr.begin(), arr.end());
arr.erase(last, arr.end());
std::for_each(arr.begin(), arr.end(), [](int n){std::cout << n << std::endl;} );
}
Try it on Godbolt.
problem is in for loop becouse you use less or equal to n.
you need to use just less then n becouse array starts from zero that means you asking for sixth element that do not have alocalizated in memory
now it should be correct
#include<iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
int n=sizeof(arr)/sizeof(arr[0]);
int c=0;
for(int j=0; j<n; j++) {
if (arr[j]!=arr[j+1]) {
cout<<arr[j];
}
}
}
Is there any problem with this approach ? or is it fine? we have to tell whether 2 arrays are equal
as in the digits in them and their frequencies must be same irrespective of their order.
#include<bits/stdc++.h>
using namespace std;
main(){
int t,n,i,in;
vector<int> x;
cin>>t;
while(t--){
unordered_map<int,int> a,b;
cin>>n;
for(i=0;i<n;i++){
cin>>in;
x.push_back(in);
a[in]++;
}
for(i=0;i<n;i++){
cin>>in;
b[in]++;
}
for(i=0;i<n;i++){
if(b.find(x[i]) == b.end()){
cout<<"0"<<endl;
goto x;
}
if(a[x[i]] != b[x[i]]){
cout<<"0"<<endl;
goto x;
}
}
cout<<"1"<<endl;
x : ;
}
}
I don't know how new you are to C++, so here is a possible solution with a few explanations.
#include <map>
#include <vector>
#include <iostream>
//Avoid using namespace std as it can cause problems with conflicts of names
//and other sorts of nasty issues
using std::cout;
using std::cin;
using std::map;
using std::vector;
using std::endl;
//By using an ordered map you can more easily compare them
//The arguments are passed as const (you will never modify them)
//and also as reference (&) since you don't need to copy them.
bool CheckIfEqual (const vector<int> & V1, const vector<int> & V2) {
//If the vectors don't have the same size you can just return false
if (V1.size() != V2.size()) {
return false;
}
map <int, size_t> M1;
map <int, size_t> M2;
//This type of loop goes through all elements of the vector and
//either creates a corrisponding value in the map or, if it is
//already present, it increases it to accout for repetitions.
//Map is automatically sorted.
for (auto & Elem : V1) {
M1[Elem]++;
}
for (auto & Elem : V2) {
M2[Elem]++;
}
return M1 == M2;
}
//The main function is used to provide some examples
int main () {
//Expected output: true
vector<int> V1 {1, 2, 3, 4, 5};
vector<int> V2 {1, 2, 3, 4 ,5};
cout << CheckIfEqual(V1, V2) << endl;
//Expected output: true
V1 = {1, 2, 3, 4, 5};
V2 = {5, 3, 2, 1 ,4};
cout << CheckIfEqual(V1, V2) << endl;
//Expected output: false
V1 = {1, 2, 3};
V2 = {5, 3};
cout << CheckIfEqual(V1, V2) << endl;
//Expected output: false
V1 = {1, 2, 3, 4, 5};
V2 = {5, 3, 2, 1 ,1};
cout << CheckIfEqual(V1, V2) << endl;
//Expected output: true
V1 = {1, 5, 5, 4, 5};
V2 = {5, 5, 5, 1 ,4};
cout << CheckIfEqual(V1, V2) << endl;
}
The output is
1
1
0
0
1
Also please be very very careful when using goto, it is deprecated and only useful when you need to jump out of many nested loops all at once. A simple break would have worked better.
I am trying to replace the elements in a 2D vector (vector<vector<int>>). I want to change the elements not only by one value, but by a list, which means, for example, change 1,3,4,5,8,9 to 1,2,3,4,5,6 one-to-one correspondence. I have made a very slow code with double loops. Is there any way to speed up the process, with new function or sort the element? Because my 2D vector is very big, 3*300000 actually. My example code is below:
int myints[] = { 1,3,4,5,8,9 };
int myints2[] = { 1,2,3,4,5,6 };
std::vector<int> vals (myints, myints+6);
std::vector<int> vals2 (myints2, myints2+6);
vector<vector<int>> V0(3);
V0[0]={1,4,5};
V0[1]={3,1,8};
V0[2]={1,9,4};
for (size_t j = 0; j < V0.size(); j++)
{
for (int i = 0; i < vals.size(); i++)
replace(V0[j].begin(), V0[j].end(), vals[i], vals2[i]);
};
The ideal output V0 should be
1 3 4
2 1 5
1 6 3
You can use an unordered_map to replace each value directly, instead of searching through the whole vector for each replacement:
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
unordered_map<int, int> replacements{{1, 1}, {3, 2}, {4, 3}, {5, 4}, {8, 5}, {9, 6}};
vector<vector<int>> v0(3);
v0[0] = {1, 4, 5};
v0[1] = {3, 1, 8};
v0[2] = {1, 9, 4};
for_each(v0.begin(), v0.end(), [&](vector<int>& v)
{
transform(v.begin(), v.end(), v.begin(), [&](int val)
{
auto it = replacements.find(val);
return it != replacements.end() ? replacements[val] : val;
});
});
// Print
for (auto& v : v0)
{
cout << "[ ";
for (auto val : v)
{
cout << val << ", ";
}
cout << "]" << endl;
}
return 0;
}
Output:
[ 1, 3, 4, ]
[ 2, 1, 5, ]
[ 1, 6, 3, ]
In C++17, you may also choose a parallel execution policy in for_each and/or transform, since all the changes can be done in parallel.
int array[6] = {5, 10, 2, 5, 4, 4}
std::sort(array, array + (sizeof array / sizeof array[0]), std::greater<int>());
I am trying to sort the above array in descending order, but when I run the sort function on it I am getting the following:
{10, 5, 2, 5, 4, 4}
Any ideas?
EDIT: The problem was with surrounding code I had. The code posted here is actually correct.. Sorry.
Based on the example of the reference, what you have should work:
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
int main (void) {
int array[6] = {5, 10, 2, 5, 4, 4};
std::sort(array, array + (sizeof array / sizeof array[0]), std::greater<int>());
for (int i = 0; i < 6; ++i)
std::cout << array[i] << " ";
std::cout << '\n';
return 0;
}
Output:
10 5 5 4 4 2
As you can see, this is the same as your code. See it yourself in the Live Demo.
In c++11 you can do this wonderful syntax:
vector<int> numbers = {1, 2, 3};
Is there a way to concatenate a further initializer list onto an existing vector?
numbers.??? ({4, 5, 6});
or
std::??? (numbers, {4, 5, 6});
You can use std::vector::insert for that:
#include <vector>
vector<int> numbers = {1, 2, 3};
numbers.insert( numbers.end(), {4, 5, 6} );
Use std::vector::insert:
numbers.insert(numbers.end(), {4, 5, 6});
You can use std::vector::insert. Link to example code
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1,2,3};
a.insert(a.end(), {4,5,6});
for(int &i : a) {
cout << i << " ";
}
cout << endl;
return 0;
}