I am practicing leetcode easy problem. I want to remove_if from an vector using lambda (for the first time, It is great). I get a negative pointer for new_end.
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional> // std::greater
using namespace std;
int main()
{
vector<int> a = { 2, 7, 11, 15 };
int target = 9;
auto new_end = std::remove_if(a.begin(), a.end(), [&a, target](const int x)
{
return std::count(a.begin(), a.end(), x) > target;
});
a.erase(new_end, a.end());
return 0;
}
There is no error but new_end is a negative pointer value.
std::remove_if(begin, end, pred) returns an iterator pointing at the first element to erase or end if there is no element matching pred. The later is true in your case:
auto new_end = std::remove_if(a.begin(), a.end(),
[&a, target](const int x) { return std::count(a.begin(), a.end(), x) > target; }
);
new_end equals a.end(). This value is printed as garbage by your debugger. But it happens to just works by chance in your case.
As pointed out by multiple commentators, once your predicate has returned true once, the range [a.begin(), a.end) is modified and the last element has an unspecified value1.
This makes std::count(a.begin(), a.end(), x) return unspecified values.
A suggested fix is to make a copy of a before remove_if starts to move things around. This is done by capturing it by value:
auto new_end = std::remove_if(a.begin(), a.end(),
[b=a, target](const int x) { return std::count(b.begin(), b.end(), x) > target; }
);
Initializing the copy to a new name b simply emphasizes that it is a copy.
1) From std::remove_if:
Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values (as per MoveAssignable post-condition).
I assume to want to remove numbers greater than 9 from vector, Here is the code
vector<int> a = { 2, 7, 11, 15 };
int target = 9;
auto new_end = std::remove_if(a.begin(), a.end(), [](const int x)
{
return x > 9;
});
a.erase(new_end, a.end());
return 0;
The lambda argument 'x' will be provided by remove_if, you don't need to add anything in the capture list
Related
I tried to remove duplicate elements from a vector by a function vectorremove, using the function remove from the library of algorithms, but it does not work:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
void vectorremove(vector<string> v)
{
for (vector<string>::iterator it = v.begin(); it != v.end(); ++it)
{
vector<string>::iterator end = remove(it + 1, v.end(), *it);
v.erase(end, v.end());
}
}
int main()
{
vector<string> vect;
string x;
while (cin >> x)
{
vect.push_back(x);
}
vectorremove(vect);
for (vector<string>::iterator it = vect.begin(); it != vect.end(); ++it)
{
cout << *it << endl;
}
return 0;
}
I wrote this code to test if the function vectorremove works, unfortunately it seems that vectorremove has no impact on the vector. Have I made any mistake in the use of remove in the definition of vectorremove?
The first problem in your code is that you are passing the vector by value and not by reference to vectorremove. You need to change that to
void vectorremove(vector<string>& v);
Then inside your vectorremove function you have another problems. vector::erase can invalidate all iterators, so you should onle remove inside the loop and do the erase after the loop.
void vectorremove(vector<string>& v)
{
vector<string>::iterator end{ v.end() };
for (vector<string>::iterator it = v.begin(); it != end; ++it)
{
end = remove(it + 1, end, *it);
}
v.erase(end, v.end());
}
First you are passing std::vector by value, not by reference. Therefore, any changes you make in vectorremove function won't be visible in main.
Furthermore, std::vector::erase might invalidate iterators so you must not use it inside the loop.
Your code could look like:
void vectorremove(std::vector<std::string>& v) {
auto end{ v.end() };
for (auto it = v.begin(); it != end; ++it)
{
end = std::remove(it + 1, end, *it);
}
v.erase(end, v.end());
}
Note the usage of auto instead of std::vector<std::string>::iterator.
However, STL provides handy functions to achieve what you want. One of them is std::unique which
Eliminates all but the first element from every consecutive group of
equivalent elements from the range [first, last) and returns a
past-the-end iterator for the new logical end of the range.
In order to remove duplicates from the std::vector, you can do something like:
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v{ 1, 2, 3, 1, 2, 3, 3, 4, 5, 4, 5, 6, 7 };
std::sort(v.begin(), v.end()); // 1 1 2 2 3 3 3 4 4 5 5 6 7
auto last = std::unique(v.begin(), v.end());
v.erase(last, v.end());
for (auto const i : v) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
Remember that std::unique works as expected only on sorted std::vectors.
You only modify the copy of vector , you have to pass by reference to modify the actual
vector, why you don't use auto instead of std::vector::iterator. and you have to
know that erase invalidates all iterators pointing to the erased element and
beyond the erased element, keep the iterator valid by using erase's return value, also use std::getline inside the loop to store the value from std::cin to include the new line.
Alternatively your can use std::unique removes the duplicate value and works as expected after sorting the elements. and std::unique returns a past the end iterator for the new logical end of the range:-
#include <vector>
#include <algorithm>
#include <string>
std::vector<std::string> removeDuplicate(std::vector<std::string> & v){
std::vector<std::string> vec;
std::sort(std::begin(v), std::end(v));
auto pos = std::unique(std::begin(v), std::end(v));
vec.assign(std::begin(v), pos);
return vec;
}
int main(){
std::vector<std::string> vect{"John", "John", "Paul", "John", "Lucy", "Bob", "Bob"};
auto pureVector = removeDuplicate(vect);
for(auto const & v : pureVector){
std::cout << v << '\n';
}
}
I want to be able to accumulate every other pair of elements in a vector using accumulate. I tried the following without much success, returning an error for a non-empty, non-zero vector
return std::accumulate(vec.begin(), vec.end(), 0,
[&](int runningSum, int first, int second)
{return runningSum = runningSum + min(first, second);});
which I now realise probably wouldn't be getting the minimum between pairs. For instance, if I have
vector<int> vec = {1,4,2,3}
I want to return 0 + min(1, 4) + min(2, 3).
On another note, is there any website with many examples of these STL built-ins? I find the examples online far and few. I really want to see the power of accumulate, and get comfortable with it.
std::accumulate() does not allow you to use a predicate with 3 parameters, only 2 parameters - the current running sum, and the current element to be added to that sum. The predicate is called for each individual element and is expected to return the updated sum.
If you want to sum the values in pairs, you can try something like this instead:
vector<int> vec = {1,4,2,3};
...
int *first = nullptr;
return std::accumulate(vec.begin(), vec.end(), 0,
[&](int runningSum, int &value) {
if (first) {
runningSum += std::min(*first, value);
first = nullptr;
} else {
first = &value;
}
return runningSum;
}
);
A better solution would be to simply change your vector to hold a pair of ints (like std::pair<int, int>) as its element type (or at least copy your vector ints to a second vector of pairs), and then you can accumulate the pairs as-is:
vector<pair<int,int>> vec = {{1,4},{2,3}};
...
return std::accumulate(vec.begin(), vec.end(), 0,
[](int runningSum, const pair<int, int> &p) {
return runningSum + std::min(p.first, p.second);
}
);
I think it would be difficult to just use accumulate straight away to sum by min of pairs. You would need to maybe split your existing vector first, then transform them into a vector of the mins, then you could use the accumulate function.
So with that in mind, I would maybe do it like this:
std::vector<int> v{ 1,4,2,3};
std::vector<int> v2;
std::vector<int> v3;
std::vector<int> v4;
std::partition_copy(begin(v),
end(v),
back_inserter(v2),
back_inserter(v3),
[toggle = false](int) mutable { return toggle = !toggle; });
std::transform(begin(v2), end(v2), begin(v3), std::back_inserter(v4), [](auto a, auto b)
{
return std::min(a,b);
});
auto sum_of_min_in_pairs = std::accumulate(begin(v4), end(v4), 0);
Note that the code above will have issues if your vector does not have even amount of elements. Otherwise, transform it into a pair, with some default to match the remainder, depending on what you want to achieve.
With the STL website, cppreference.com is your friend. There are also a few books around I would highly recommend.
Effective STL by Scott Meyers
The C++ standard library by Nicolai Josuttis
I would solve this using std::adjacent_difference and std::accumulate:
#include <algorithm> // std::min
#include <iostream>
#include <numeric> // std::adjacent_difference, std::accumulate
#include <vector>
int main()
{
std::vector v{1, 4, 3, 2, 3, 8, 5, 1};
std::adjacent_difference(
v.cbegin(), v.cend(), v.begin(),
[](auto lhs, auto rhs) { return std::min(lhs, rhs); });
auto is_second_elem{true};
std::cout << std::accumulate(cbegin(v), cend(v), 0,
[&is_second_elem](auto acc, auto min) {
is_second_elem = not is_second_elem;
return is_second_elem ? (acc + min) : acc;
})
<< '\n'; // 7
}
I have this requirement to find the last element in the vector which is smaller than a value.
Like find_first_of but instead of first i want last.
I searched and found that there is no find_last_of but there is find_first_of.
Why is that so? Is the standard way is to use find_first_of with reverse iterators?
Use reverse iterators, like this:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{1,2,42,42,63};
auto result = std::find_if(v.rbegin(), v.rend(),
[](int i) { return i == 42; });
std::cout << std::distance(result, v.rend()) << '\n';
}
Live demo.
This is how it is done with reverse iterators:
std::vector<int> vec = {2,3,10,5,7,11,3,6};
//below outputs '3':
std::cout << *(std::find_if(vec.rbegin(), vec.rend(), [](int i) { return i < 4; }));
Just one thing. Be careful with the predicate if you're looking to find the tail-end of the range which includes the predicated element:
int main()
{
std::vector<int> x { 0, 1, 2, 3, 4, 5 };
// finds the reverse iterator pointing at '2'
// but using base() to convert back to a forward iterator
// also 'advances' the resulting forward iterator.
// in effect, inverting the sense of the predicate to 'v >= 3'
auto iter = std::find_if(std::make_reverse_iterator(x.end()),
std::make_reverse_iterator(x.begin()),
[](auto& v) { return v < 3; }).base();
std::copy(iter,
x.end(),
std::ostream_iterator<int>(std::cout, ", "));
}
result:
3, 4, 5,
From ZenXml:
template <class BidirectionalIterator, class T> inline
BidirectionalIterator find_last(const BidirectionalIterator first, const
BidirectionalIterator last, const T& value)
{
for (BidirectionalIterator it = last; it != first;)
//reverse iteration: 1. check 2. decrement 3. evaluate
{
--it; //
if (*it == value)
return it;
}
return last;
}
Consider the following scenario:
typedef struct myStruct
{
int cn;
std::string dn;
} MyStruct;
int main()
{
std::vector<MyStruct> v;
// fill some data
...
...
int c = 1;
std::vector<MyStruct>::iterator it = std::find_if(v.begin(), v.end(),
[c](const MyStruct& m) -> bool { return m.cn == c; });
// use 'it' to do stuff
}
If v contains MyStruct objects such that the member variable cn has a value c (=1) in more than one entries, how to handle that scenario? As std::find_if() returns an iterator to the first element in the range, what about the rest?
find_if find first element in range and returns iterator to it. For find all you can either write loop, that will search each-time from it:
std::vector<MyStruct>::iterator it = v.begin();
while (it != v.end())
{
it = std::find_if(it, v.end(),
[c](const MyStruct& m) -> bool { return m.cn == c; });
if (it != v.end())
{
// process founded item
++it;
}
}
or you can sort your sequence and use equal_range algorithm, that will return std::pair of iterators.
With the current Standard library, you have to write a either a loop over std::find_if with a predicate (lambda if you can use C++11/14), or use std::copy_if to copy every match to a new sequence.
When the Ranges proposal becomes available (in a Technical Specification along with C++17), things get much easier, e.g. you will be able to write one single chain of composable views and actions:
#include <range/v3/all.hpp>
#include <iostream>
#include <vector>
using namespace ranges;
int main()
{
auto const is_even = [](auto x) { return x % 2 == 0; };
auto const print = [&](auto x) { std::cout << x << ","; return x; };
std::vector<int> v { 1, 11, 42, 57, 63, 72 };
v | view::filter(is_even) | action::transform(print);
}
Live On Coliru (already works with the range-v3 library).
I know we need to include some compare function in order to achieve this.
But not able to write for this one.
For example:
Elements of vector={(2,4),(4,2),(5,1),(5,3)}
to find=5
lower_bound() should return 2
code->
#define pp pair<int,int>
bool cmp(const pp &l,const pp &r) {
return l.first < r.first;
}
int main() {
vector<pp> v;
sort(v.begin(), v.end(), cmp);
int id=(int)(lower_bound(v.begin(), v.end(), ??) - v.begin());
}
Pairs (just like tuples) compare lexicographically anyway. You don't need to define any special comparators for this.
And since you're using lower_bound you'll be searching for the first element that does not compare less than the val you're searching, so you should use a min value as the second pair element. To sum up, all can be done in "two" lines of code :
sort(v.begin(),v.end());
auto id = distance(v.begin(), lower_bound(v.begin(),v.end(),
make_pair(5, numeric_limits<int>::min())) );
Some Notes :
Use std::distance to calculate the number of elements between two iterators
The return type of std::distance is an unsigned type. Unless you need negative indexing (Python like syntax for "count from the end" indexes) it's a good practice to keep your indexes unsigned.
Since you don't care about the second value of pp, just construct a temporary pp object with any value as the second element.
int id = std::lower_bound(v.begin(), v.end(), pp(5, 0), cmp) - v.begin();
I think you should compare the pairs as per definition of lower_bound
So,
typedef pair<int,int> pp;
//...
int id=(int)(lower_bound(v.begin(),v.end(),
pp(5,std::numeric_limits<int>::min())), //Value to compare
[](const pp& lhs, const pp& rhs) // Lambda
{
return lhs < rhs ; // first argument < second
}
) - v.begin()
);
You can use lower_bound on vector of pairs with custom compare operator .
You need to pass four arguments in that case like this :-
it1 = iterator position from where to search
it2 = iterator position till where to search
lower_bound (it1 ,it2 , finding_element, your_comparator )
auto myComp = [&](pair<int,string> e1, pair<int,string> e2) {
if(e1.second!=e2.second)
return e1.second<e2.second;
else
return e1.first<e2.first;
};
void Show_sample_code()
{
vector<pair<int,string>> data={{1, "sahil"}, {2, "amin"}};
sort(data.begin(), data.end(), myComp);
pair<int, string> p={1,"sahil"};
auto it=lower_bound( data.begin(), data.end(), p, myComp ) ;
if(it!=data.end())
cout<<"found at index="<<distance(data.begin(), it)<<endl;
else
cout<<"notfound"<<endl;
return;
}