I'm currently writing a code that receives a string
as an input, checking whether or not it is comprised of only chars from the car_acc
array and, if it isn't the case, requesting the input once again, using a do-while loop.
This is the code:
char car_acc [11] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ':'};
deque <bool> contr_car;
do
{
cout << "Inserire il tempo di attesa (ore:minuti:secondi) -> ";
cin >> tempo; fflush (stdin);
lung = (int) tempo.length ()-1;
for (; i != lung; i =1)
{
contr_car [i] = false;
}
for (i = 0; i != 11; i =1)
{
if (car_acc [i] == tempo [j])
{
contr_car [j] = true;
}
}
}
while (all_of (contr_car [0], contr_car [lung], true) == false);
I'm using the contr_car
deque to store whether or not each character in the string
is equal to one of those stored in the car_acc
array.
I'm receiving this error, linking in the algorithm
header file:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c /v1/algorithm:926:21: Indirection requires pointer operand ('int' invalid)
The error occurs in the part of the file that is defining the all_of
function:
template <class _InputIterator, class _Predicate>
inline _LIBCPP_INLINE_VISIBILITY
bool
all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
{
for (; __first != __last; __first)
if (!__pred(*__first)) // error's here
return false;
return true;
}
I also get this message on the same line as the while
:
In instantiation of function template specialization 'std::__1::all_of<bool, bool>' requested here
Using the find ()
function in place of all_of ()
doesn't fix the error.
I'm using a deque since I had to declare it outside of the loop, inside of which I get the size, so arrays weren't an option, and I don't really know how to use vector <bool>
, since it works as a bitset.
I did include both the algorithm
and deque
headers.
I tried using a vector <int>
but it still doesn't work, it only changes the message I get on the line where the while
is at:
In instantiation of function template specialization 'std::__1::all_of<int, int>' requested here
I honestly have no clue how to fix it, since I'm just getting started with C and coding in general, so I would be really greatful if you could phrase your anwsers in a easy-to-understand way.
CodePudding user response:
The algorithm std::all_of
is intended to invoke a unary predicate on every element within an iteration sequence, reaping either 'true' or 'false' from each, and answering with one encompassing 'true' (everything reported true) or 'false' (at least one thing reported false). To do this, you supply three things:
- A beginning iterator
- An ending iterator
- A callable unary predicate returning a bool.
Your code supplies exactly none of those, and therefore it is no wonder it won't compile. An example of how you can use std::all_of
is shown below. It isn't intended to be cookie-cutter, so please don't think otherwise.
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <string>
int main()
{
const std::unordered_set<char> car_acc = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ':'};
std::string tempo;
do
{
std::cout << "Inserire il tempo di attesa (ore:minuti:secondi) -> ";
std::cout.flush();
if (!(std::cin >> tempo))
{
// TODO: handle failure on stdin
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
} while ( !std::all_of(tempo.begin(), tempo.end(),
[&car_acc](char c){return car_acc.find(c) != car_acc.end();}) );
std::cout << "You entered: " << tempo << '\n';
}
This simply creates an unordered set containing what you've deemed are "allowable" characters, then uses that set as the basis for the unary predicate here, a simple lambda:
[&car_acc](char c){return car_acc.find(c) != car_acc.end();}
This is then fed through std::all_of
using the string begin and end iterators. If any character in the string fails to find a match in the set, the result of that match-check will be false, and therefore shall also be the result of std::all_of
.