Using std::lower_bound with std::vector::const_iterator - c++

I'm trying to find bounds in the part of vector from next iterator position to the end of the vector.
The code is:
#include <algorithm>
#include <vector>
#include <iostream>
int
main()
{
typedef std::vector<int> Vector;
Vector v;
v.push_back(3);
v.push_back(7);
v.push_back(15);
v.push_back(21);
std::sort(v.begin(), v.end());
for (Vector::const_iterator i = v.begin(); i != v.end(); ++i) {
Vector::const_iterator low = std::lower_bound(i+1, v.end(), -10-*i);
Vector::const_iterator high = std::upper_bound(i+1, v.end(), 10-*i);
for (Vector::const_iterator j = low; j != high; ++j) {
std::cout << *i << "~" << *j << std::endl;
}
}
return 0;
}
Unfortunately, std::lower_bound(i+1, v.end(), -10-*i) triggers compilation error which I cannot understand:
c++ -O3 -ggdb -std=c++11 -W -Wall -pedantic -Wl,-stack_size -Wl,0x1000000 2sum.cc -o 2sum
2sum.cc:26:26: error: no matching function for call to 'lower_bound'
V::const_iterator lo = std::lower_bound(i+1, a.end(), -1...
^~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4085:1: note:
candidate template ignored: deduced conflicting types for parameter
'_ForwardIterator' ('__wrap_iter<const long *>' vs.
'__wrap_iter<long *>')
lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp...
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:4070:1: note:
candidate function template not viable: requires 4 arguments, but 3 were
provided
lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp...
^
What is the problem with the statement above and how to properly call std::lower_bound with i+1 iterator?

The error probably says that it can't deduce the template argument - i is const_iterator while v.end() returns iterator. This should do the trick:
std::lower_bound( i+1, v.cend(), -10-*i);
^^^^^^^^

Related

How to call std::unique with custom predicate in C++03?

I saw this example of how to do it in C++11:
std::unique(v.begin(), v.end(), [](float l, float r)
{
return std::abs(l - r) < 0.01;
});
However, this fails for me in C++03:
error: template argument for 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)' uses local type 'CRayTracer::myFunc()::<lambda(float, float)>'
How can I do this in C++03? I think Lambdas may have already existed, and functors/function objects existed, right? Just looking for a simple solution, does not need to be extendable - it will only ever be used here.
Here is an example of code which will not compile for me:
#include <iostream>
#include <vector>
#include <algorithm>
int main(){
std::vector<float> v;
v.push_back(1.0);
v.push_back(1.0);
v.push_back(3.5);
v.push_back(3.5);
struct approx_equal
{
bool operator()(float l, float r)
{
return std::abs(l - r) < 0.01;
}
};
approx_equal f;
std::unique(v.begin(), v.end(),f);
}
Here's the error it produced:
testUnique.cpp: In function 'int main()':
testUnique.cpp:21:37: error: no matching function for call to 'unique(std::vector<float>::iterator, std::vector<float>::iterator, main()::approx_equal&)'
21 | std::unique(v.begin(), v.end(),f);
| ^
In file included from C:/msys64/mingw64/include/c++/9.2.0/algorithm:62,
from testUnique.cpp:3:
C:/msys64/mingw64/include/c++/9.2.0/bits/stl_algo.h:995:5: note: candidate: 'template<class _FIter> _FIter std::unique(_FIter, _FIter)'
995 | unique(_ForwardIterator __first, _ForwardIterator __last)
| ^~~~~~
C:/msys64/mingw64/include/c++/9.2.0/bits/stl_algo.h:995:5: note: template argument deduction/substitution failed:
testUnique.cpp:21:37: note: candidate expects 2 arguments, 3 provided
21 | std::unique(v.begin(), v.end(),f);
| ^
In file included from C:/msys64/mingw64/include/c++/9.2.0/algorithm:62,
from testUnique.cpp:3:
C:/msys64/mingw64/include/c++/9.2.0/bits/stl_algo.h:1025:5: note: candidate: 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)'
1025 | unique(_ForwardIterator __first, _ForwardIterator __last,
| ^~~~~~
C:/msys64/mingw64/include/c++/9.2.0/bits/stl_algo.h:1025:5: note: template argument deduction/substitution failed:
testUnique.cpp: In substitution of 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate) [with _FIter = __gnu_cxx::__normal_iterator<float*, std::vector<float> >; _BinaryPredicate = main()::approx_equal]':
testUnique.cpp:21:37: required from here
testUnique.cpp:21:37: error: template argument for 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)' uses local type 'main()::approx_equal'
21 | std::unique(v.begin(), v.end(),f);
| ^
testUnique.cpp:21:37: error: trying to instantiate 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)'
And here was my set of flags:
g++ -c -g -O3 -Wp,-D_FORTIFY_SOURCE=2 -m64 -Wshadow -Wall -DMX_COMPAT_32 -fexceptions -fno-omit-frame-pointer -D__WIN32__ -std=c++03 testUnique.cpp -o testUnique.o
I think Lambdas may have already existed,
No. Lambdas have been introduced in C++11.
... and functors/function objects existed, right?
Functors are simply objects with an operator(), so they always have been there (though not sure when the term "functor" was actually introduced for them).
For the formal correct way to put it I refer you to other references, sloppy speaking this
auto f = [](float l, float r){
return std::abs(l - r) < 0.01;
};
f(0.1,0.2);
Is equivalent to
struct unnamed {
bool operator()(float l, float r) {
return std::abs(l - r) < 0.01;
}
};
unnamed f;
f(0.1,0.2);
I.e. you can always replace a lambda with a handwritten functor class. Create an instance of the functor and pass that instead of the lambda.
Complete example:
#include <iostream>
#include <vector>
#include <algorithm>
struct approx_equal{
bool operator()(float l, float r) {
return std::abs(l - r) < 0.01;
}
};
int main(){
std::vector<float> v{1.0, 1.0, 3.5, 3.5 };
approx_equal f;
v.erase(std::unique(v.begin(), v.end(),f),v.end());
// sorry this is c++11, replace with iterator loop to see output pre-c++11
for (const auto& x : v) std::cout << x << " ";
}
PS: In C++03 you cannot define the functor class locally and then use it as template parameter (note you do not explicitly pass it as template parameter, but unique has to deduce its type from the parameter you pass in).
Lambdas don't exist in C++03, they were introduced in C++11.
Since your lambda example does not need to capture values, you can replace it with a simple standalone function, you don't need a functor, eg:
#include <iostream>
#include <vector>
#include <algorithm>
bool approx_equal(float l, float r) {
return std::abs(l - r) < 0.01;
}
int main(){
std::vector<float> v;
v.push_back(1.0);
v.push_back(1.0);
v.push_back(3.5);
v.push_back(3.5);
std::unique(v.begin(), v.end(), approx_equal);
}
However, you can use a functor, too. In your attempt to use a functor, you simply defined the functor type inside of main() itself, making it a local type, which the compiler complained about. Define the functor type in global scope instead (you can still use a local variable to instantiate the type), eg:
#include <iostream>
#include <vector>
#include <algorithm>
struct approx_equal {
bool operator()(float l, float r) {
return std::abs(l - r) < 0.01;
}
};
int main(){
std::vector<float> v;
v.push_back(1.0);
v.push_back(1.0);
v.push_back(3.5);
v.push_back(3.5);
approx_equal f;
std::unique(v.begin(), v.end(), f);
// or:
// std::unique(v.begin(), v.end(), approx_equal{});
}

Error using for_each and lambda

I am trying to solve a simple Fibonacci problem using the lambda function, but I came across this error and I cant solve it.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
string str;
auto N{0};
auto i{0};
cout<<"Digite o valor de N desejado: ";
getline(cin,str); //pega linha
stringstream(str) >> N;
if (N == 0){cout<<0;}
else if (N == 1){cout<<1;}
else
{
vector<int> v{0,1}; //cria vetor v
for_each(v.begin(),N,
[&](){
v.push_back(v[i]+v[i+1]);
i++;
});
i = 0;
for_each(v.begin(),N,
[&](){
cout<<v[i];
i++;
});
}
return 0;
}
And the error is the following:
quest1.cpp: In function ‘int main()’: quest1.cpp:30:4: error: no matching function for call to ‘for_each(std::vector<int>::iterator, int&, main()::<lambda()>)’ });
^ In file included from /usr/include/c++/6.3.1/algorithm:62:0,
from quest1.cpp:5: /usr/include/c++/6.3.1/bits/stl_algo.h:3763:5: note: candidate: template<class _IIter, class _Funct> _Funct std::for_each(_IIter,
_IIter, _Funct)
for_each(_InputIterator __first, _InputIterator __last, _Function __f)
^~~~~~~~ /usr/include/c++/6.3.1/bits/stl_algo.h:3763:5: note: template argument deduction/substitution failed: quest1.cpp:30:4: note: deduced conflicting types for parameter ‘_IIter’ (‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ and ‘int’) });
^ quest1.cpp:38:5: error: no matching function for call to ‘for_each(std::vector<int>::iterator, int&, main()::<lambda()>)’
});
^ In file included from /usr/include/c++/6.3.1/algorithm:62:0,
from quest1.cpp:5: /usr/include/c++/6.3.1/bits/stl_algo.h:3763:5: note: candidate: template<class _IIter, class _Funct> _Funct std::for_each(_IIter,
_IIter, _Funct)
for_each(_InputIterator __first, _InputIterator __last, _Function __f)
^~~~~~~~ /usr/include/c++/6.3.1/bits/stl_algo.h:3763:5: note: template argument deduction/substitution failed: quest1.cpp:38:5: note: deduced conflicting types for parameter ‘_IIter’ (‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ and ‘int’)
});
There's two problems in your code. One (already covered by other answers), you're passing the number of iterations as the second parameter to std::for_each, but it actually expects an end iterator. std::for_each(a, b, f) is designed to iterate from iterator a to iterator b and call f on each element in that range.
The second problem is more fundamental: if you modify the vector while iterating over it, you'll get Undefined Behaviour because any modification operation invalidates all iterators to the vector.
Looking at your code, it seems you want the first loop to a normal counting loop and not iteration over the container. The second loop could be done with for_each and a lambda:
vector<int> v{0,1};
for (int i = 0; i < N; ++i) {
v.push_back(v[i] + v[i+1]);
}
for_each(v.begin(), v.end(),
[](int element) {
cout << element;
}
);
Notice that the functor used in for_each is not nullary: the algorithm will pass the element to it.
Alternatively, the printing functionality could be implemented without lambdas:
copy(v.begin(), v.end(), ostream_iterator<int>(cout));
Or, if you prefer to keep the loop, you could just as well use a range-based for loop instead of for_each. The code will be shorter and [subjective]easier to read[/subjective]:
for (int elem : v) {
std::cout << elem;
}
std::for_each requires (as the error tries to tell you) an iterator where to start looping and an iterator where to end. You however are passing an iterator and an int, which are "conflicting types". If you want to loop over an whole vector do something like that:
std::vector<int> v{1, 2, 3};
std::for_each(v.begin(), // start at the front
v.end(), // loop over each element
[&] (int& i) {
i++;
});
If you just need to loop over a part of the vector do that
std::vector<int> v{1, 2, 3};
std::for_each(v.begin(), // start at the front
v.begin() + 2, // loop over the first two elements
[&] (int& i) {
i++;
});
You are not using the for_each function correctly. C++ does not know the signature you are using. Please refer to the documentation, e.g. for_each.
The compiler says exactly that. The second parameter of the for_each you are calling is of type int&, but the signature is for_each(InputIterator, InputIterator, Function) and not for_each(InputIterator, int&, Function), so it fails to compile.

How to call a template function correctly?

I am new to template function and cannot figure this error. Hope you can help.
#include <iostream>
#include <vector>
/*
* Print to the screen the content of a vector
* Define function template in the header
*/
template <typename T> void print_vector(T& v) {
for(typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i)
std::cout << *i << ' ';
}
int main() {
std::vector<int> field;
field.resize(12, 1);
/*
for( std::vector<int>::const_iterator i = field.begin(); i != field.end(); ++i)
std::cout << *i << ' ';
*/
print_vector(field);
}
My code failed to compile with a very long error message that I can't even insert here.
error: conversion from ‘std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’ to non-scalar type ‘std::vector<std::vector<int>, std::allocator<std::vector<int> > >::const_iterator {aka __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int>, std::allocator<std::vector<int> > > >}’ requested
utility.h:21:59: error: no match for ‘operator!=’ in ‘i != (& v)->std::vector<_Tp, _Alloc>::end<int, std::allocator<int> >()’
utility.h:21:59: note: candidates are:
In file included from /usr/include/x86_64-linux-gnu/c++/4.7/./bits/c++allocator.h:34:0,
from /usr/include/c++/4.7/bits/allocator.h:48,
from /usr/include/c++/4.7/string:43,
from /usr/include/c++/4.7/bits/locale_classes.h:42,
from /usr/include/c++/4.7/bits/ios_base.h:43,
from /usr/include/c++/4.7/ios:43,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from utility.h:4:
/usr/include/c++/4.7/ext/new_allocator.h:134:5: note: template<class _Tp> bool __gnu_cxx::operator!=(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&)
/usr/include/c++/4.7/ext/new_allocator.h:134:5: note: template argument deduction/substitution failed:
utility.h:21:59: note: ‘std::vector<std::vector<int>, std::allocator<std::vector<int> > >::const_iterator {aka __gnu_cxx::__normal_iterator<const std::vector<int>*, std::vector<std::vector<int>, std::allocator<std::vector<int> > > >}’ is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’
When you call
std::vector<int> field;
...
print_vector(field);
the T in print_vector1 is the type of field, i.e., std::vector<int>. Therefore the typename std::vector<T>::const_iterator in
for(typename std::vector<T>::const_iterator i = v.begin();
is a std::vector<std::vector<int> >::const_iterator, to which v.begin() (itself being a std::vector<int>::iterator) is not convertible. Use
for(typename T::const_iterator i = v.begin();
instead.
1 That is to say: in the function that's made from the function template print_vector for this case.
In your function:
template <typename T>
void print_vector(T& v) {
for(typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i)
std::cout << *i << ' ';
}
T is deduced as std::vector<int>, so you're trying to convert a std::vector<int>::iterator (the result of v.begin()) into a std::vector<std::vector<int>>::const_iterator (the type of i), which is not a valid conversion.
Instead, just make a more specialized template function:
template <typename T>
void print_vector(std::vector<T>& v) {
// as before
}
Either replace std::vector<T>::const_iterator i with simply T::const_iterator i if you want to pass it any iterable, or specialize it appropriately with void print_vector(std::vector<T>& v) if you wish for it to only work against vectors of any types.
If you run a substitution as you have it now you will see you are giving a vector to something creating a vector out of the type, hence getting a vector of vector.
Also GCC 4.8 and 4.9 have improved messages and add a more precise error caret.
If you can't move compiler, which is understandable, then you can always feed small snippets like that to an online one like ideone.com. The error would most likely have been a lot more concise and readable in 4.8.
Oh, and if you have access to C++11 features (I don't remember what made it in 4.7) you can auto those type declarations AND use something like cbegin() instead of begin to get the type resolution to pick a const_iterator instead of a normal one. If you're learning about templates, and have no work environment constraints, I would strongly recommend a move to C++11 and a more recent GCC, it will make easing in a lot easier.
You called the function properly. But then you confused the template-argument (type of the container) with the containers element-type.
Anyway, there's no reason for it not being const.
Thus, it should look like this instead:
template <typename T> void print_vector(const T& v) {
for(typename T::const_iterator i = v.begin(); i != v.end(); ++i)
std::cout << *i << ' ';
}
Or using the standard-library and lambdas:
template <typename T> void print_vector(const T& v) {
using std::begin; // Using ADL for begin and end to support raw arrays
using std::end;
std::for_each(begin(v), end(v), [](auto&& x){std::cout<<*i<<' ';});
}
Or using auto and range-for-loops (my favorite):
template <typename T> void print_vector(const T& v) {
for(auto&& i : v)
std::cout << *i << ' ';
}
Or using C++1z (projected for 2017):
void print_vector(const auto& v) {
for(auto&& i : v)
std::cout << *i << ' ';
}

c++11 sorting list using lambda

While practicing the use of lambdas, I wrote this program which is supposed to sort a list of pairs by their second element (an int).
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
list<pair <string, int>> s = {{"two", 2}, {"one", 1}, {"three", 3}};
sort(s.begin(), s.end(), [](pair<string,int> a, pair<string, int> b) -> bool {
return (a.second) > (b.second);
});
for_each(s.begin(), s.end(), [](pair<string, int> a) {
cout << a.first << " " << a.second << endl;
});
}
I get those errors, though:
c:\qt\qt5.2.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_algo.h:5513: error: no match for 'operator-' (operand types are 'std::_List_iterator<std::pair<std::basic_string<char>, int> >' and 'std::_List_iterator<std::pair<std::basic_string<char>, int> >')
std::__lg(__last - __first) * 2, __comp);
^
c:\qt\qt5.2.0\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.8.0\include\c++\bits\stl_algo.h:2245: ошибка: 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_List_iterator<std::pair<std::basic_string<char>, int> >; _Compare = main()::__lambda0]', declared using local type 'main()::__lambda0', is used but never defined [-fpermissive]
__final_insertion_sort(_RandomAccessIterator __first,
^
What is wrong with my code?
You may not use std::sort with sequential containers such as std::list or std::forward_list because they have no random access iterator that is required by the standard algorithm std::sort. By this reason the both containers have their own member functions sort.
In you case the code will look the following way:
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main()
{
list<pair <string, int>> s = {{"two", 2}, {"one", 1}, {"three", 3}};
s.sort( []( const pair<string,int> &a, const pair<string,int> &b ) { return a.second > b.second; } );
for ( const auto &p : s )
{
cout << p.first << " " << p.second << endl;
}
}
Take into account that you need to include header <string> otherwise your program will not be compiled with other compilers.
std::sort requires random access iterators, which std::list does not have. But you can use std::list::sort instead.
s.sort([](const pair<string,int>& a, const pair<string,int>& b)
{
return (a.second) > (b.second);
});
where I have made the parameters of the predicate const references, since there is no need to copy them, and doing so might incur some unnecessary overhead.

map, lambda, remove_if

So, i've problem with std::map, lambda and stl algorithm(remove_if). Actually, same code with std::list or std::vector works well.
My test example :
#include <map>
#include <iostream>
#include <algorithm>
struct Foo
{
Foo() : _id(0) {}
Foo(int id) : _id(id)
{
}
int _id;
};
typedef std::map<int, Foo> FooMap;
int main()
{
FooMap m;
for (int i = 0; i < 10; ++i)
m[i + 100] = Foo(i);
int removeId = 6;
// <<< Error here >>>
std::remove_if(m.begin(), m.end(), [=](const FooMap::value_type & item) { return item.second._id == removeId ;} );
for (auto & item : m )
std::cout << item.first << " = " << item.second._id << "\n";
return 0;
}
Error message :
In file included from /usr/include/c++/4.6/utility:71:0,
from /usr/include/c++/4.6/algorithm:61,
from main.cxx:1:
/usr/include/c++/4.6/bits/stl_pair.h: In member function ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_T1, _T2>&&) [with _T1 = const int, _T2 = Foo, std::pair<_T1, _T2> = std::pair<const int, Foo>]’:
/usr/include/c++/4.6/bits/stl_algo.h:1149:13: instantiated from ‘_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = std::_Rb_tree_iterator<std::pair<const int, Foo> >, _Predicate = main()::<lambda(const value_type&)>]’
main.cxx:33:114: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:156:2: error: assignment of read-only member ‘std::pair<const int, Foo>::first’
I don't understand what's wrong here. So, i gladly to read some advices/directions about it. My goal - use new lambda-style with std::map and algorithms, such as remove_if.
g++ 4.6, -std=c++0x.
The problem is that std::map<K,V>::value_type is std::pair<const K, V>, aka .first is const and not assignable. Lambdas have nothing to do with the problem here.
std::remove_if "removes" items by moving the elements of the container around, so that everything that does not fit the predicate is at the front, before the returned iterator. Everything after that iterator is unspecified. It does that with simple assignment, and since you can't assign to a const variable, you get that error.†
The name remove can be a bit misleading and in this case, you really want erase_if, but alas, that doesn't exist. You'll have to make do with iterating over all items and erasing them by hand with map.erase(iterator):
for(auto it = map.begin(), ite = map.end(); it != ite;)
{
if(it->second._id == remove_id)
it = map.erase(it);
else
++it;
}
This is safe because you can erase individual nodes in the tree without the other iterators getting invalidated. Note that I did not increment the iterator in the for loop header itself, since that would skip an element in the case where you erase a node.
† By now, you should have noticed that this would wreak havoc in the std::map's ordering, which is the reason why the key is const - so you can't influence the ordering in any way after an item has been inserted.
You could use find and erase for the map. It's not as convenient as remove_if, but it might be the best you've got.
int removeId = 6;
auto foundIter = m.find(removeId);
// if removeId is not found you will get an error when you try to erase m.end()
if(foundIter != m.end())
{
m.erase(foundIter);
}