Array of functions returning bool [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have these boolean functions that I am writing individually and I was thinking to create an array and then use a loop to go over each of them. Below are individual functions that I would like to place in the array.
bool A(void);
bool E(void);
bool O(void);
bool P(void);
bool U(void);
bool I(void);
bool C(void);
bool L(void);
bool D(void);
Can I do this?

You can have an array of std::functions (which is a generalized function pointer). Sample program:
#include <array>
#include <functional>
#include <iostream>
typedef bool Func(void);
Func A,E,O,P,U,I,C,L,D;
int main()
{
std::array<std::function<Func>, 9> arr = { A,E,O,P,U,I,C,L,D };
for ( auto&& f: arr )
std::cout << f() << '\n';
}
You will need to provide bodies for all of those functions of course.
Prior to C++11 the code would have been:
Func *arr[] = { A,E,O,P,U,I,C,L,D };
for (size_t i = 0; i != sizeof arr / sizeof arr[0]; ++i)
std::cout << arr[i]() << '\n';
Using modern C++ code gives you more safety and flexibility, so it is to be preferred if you have a modern compiler available.

Related

Write a program to elaborate the concept of function overloading using pointers as a function arguments? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I know what overloaded functions are but I don’t know how to elaborate it using pointers. If someone can give me a basic program to elaborate function overloading using pointer as a function argument.
As the comments stated, it's not entirely clear where your problem is. It would be nice if you included an example for what you want to understand better, anyway, here's an example:
#include <iostream>
void foo(int x) {
std::cout << x << std::endl;
}
void foo(int* x) {
std::cout << (*x + 1) << std::endl;
}
int main() {
int x = 4;
foo(x); // prints 4
foo(&x); // prints 5
return 0;
}

C++ using std::string - why? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wanted to ask,
how does string::string operator function, I know that it is a standard constructor, for using strings, yet what does operator do? Does it allow me to use the multiplier operator at the end? Size_t represents the size of an object and string& is a pass by reference. How are these concepts making sense?
#include <iostream>
#include <string>
using namespace std::literals::string_literals;
std::string operator*(std::size_t n, const std::string& s)
{
std::string ret;
while (n--)
ret += s;
return ret;
}
int main()
{
std::cout << 5 * std::string("Hallo") << std::endl;
std::cout << 5 * "Test"s << std::endl;
}
What does std::string ret mean, can I use it because of std::string? Because std::string has been defined at the beginning ?
By implementing operator*, you allow type size_t to be "multiplied" by type string. The reason multiplied is in quotes is because you implement yourself what "multiply" means. In this particular implementation, the string is just appended to itself n times.
So 5 * std::string("Hallo") will result in HalloHalloHalloHalloHallo

What did I do wrong? C++ Newbie here [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I thought the output would be 70? (20+20+20+10=70) Why is it so large?
#include <iostream>
using namespace std;
int main()
{
int a,b,c=20;
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
The issue is that you are not initializing the variables a and b. That means when you attempt to run your program, the computer is looking in memory for a value to use for each, and that number could be very big or very small. Try this:
#include <iostream>
using namespace std;
int main()
{
int a = 20,b = 20,c=20; //here, a and b are defined
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
C is the only one variable that you initialize to 20, the other 2 variables
(a and b) are holding garbage..
so your math calculation is undefined behaviour.

How to pass bitsets with different sizes to a function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I want to pass bitsets to a function. What size should I assign to the bitset parameter bits in the function prototype if the bitsets have different sizes?
For example:
bitset<3> a;
bitset<4> b;
void ABC(bitset<size> bits){
...
}
ABC(a);
ABC(b);
You can templatize the function taking bitset as argument.
template <size_t bitsetsize>
void ABC(bitset<bitsetsize> a) {
...
}
This templatized function would be generated by compiler only when you use it somewhere in your code. If you use this function for bitsets of different sizes, separate functions would be instantiated for once for each size. So you should take care to avoid code depending on any local state variables (static variables local to function) as the function instances are different.
It is advisable to use a reference or constant reference to avoid object copy.
template <size_t bitsetsize>
void ABC(const bitset<bitsetsize> &a) {
...
}
An alternative which may not be fit for your requirements is to use std::vector<bool> instead of std::bitset if possible.
This is not possible with STL bitset.
std::bitset<N> template requires a fixed size in advance (at compile-time)
However, one way you can do this by using boost::dynamic_bitset
Something like following:
#include <iostream>
#include <boost/dynamic_bitset.hpp>
void ABC(boost::dynamic_bitset<> &a)
{
/* for (boost::dynamic_bitset<>::size_type i = 0;
i < a.size(); ++i)
std::cout << a[i]; */
std::cout << a << "\n";
}
int main()
{
std::size_t size= 5; // take any value for 'size' at runtime
boost::dynamic_bitset<> x(size); // all 0's by default
x[0] = 1;
x[1] = 1;
x[4] = 1;
ABC( x );
return 0;
}
See here

C++ typeid.name() only returns char "c" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm not entirely sure what is going on here. I'm guessing because my input is a string and I'm cycling through it one character at a time it is always returning as type char.
I pretty sure a string is actually char*. The only way I can think to fix this is to include and check what type of character it is, but I'd like to avoid doing that. Is there an alternative method using typeid.name() to figure out what the char is?
I'm using gcc compiler
voidQueue outQueue;
string temp = "32ad1f-31f()d";
int i = 0;
while(temp[i] != '\0')
{
outQueue.enqueue(temp[i]);
i++;
}
template<typename T>
void voidQueue::enqueue(T data)
{
T *dataAdded = new T;
*dataAdded = data;
string type(typeid(data).name());
cout<< type;
myQueue::enqueue((void *)dataAdded,type);
}
i want it recognize that char('9') is actually an int
You can use std::isdigit for this:
#include <cctype>
bool digit = std::isdigit(static_cast<unsigned char>(temp[i]);
In your example, T is char and gcc returns "c" for typeid(char).name(), as demonstrated by the following program:
#include <iostream>
#include <typeinfo>
int main() {
std::cout << typeid(char).name() << std::endl;
std::cout << typeid(short).name() << std::endl;
std::cout << typeid(int).name() << std::endl;
std::cout << typeid(long).name() << std::endl;
}
On my compiler, this prints out
c
s
i
l
Given that the name() strings are implementation-defined, this is compliant behaviour.