comparing functions in c++, short way? - c++

I've been recently working on a program which consists basically of 24 variations of one function(below). Everything gets executed perfectly apart from the part where I try to compare functions(with eachother). I found out that it is possible to be done by writing 24 if-else statements, yet I am certain there is a shorter way. I've also tried with vectors but no luck for now. Thanks for any help!
one of 24 functions:
int funk1()
{
ifstream myfile ("file.txt");
string line;
int i;
class1 obj1;
obj1.atr1= "Somename";
obj1.atr2="GAATTC";
while (getline(myfile, line))
{
i = countSubstring(line, obj1.atr2);
obj1.sum += i;
};
cout<<obj1.sum<<": "<<obj1.atr1<<"\n";
return obj1.sum;
}
The main function:
int main(){
funk1();
funk2();
funk3();
funk4();
funk5();
funk6();
funk7();
funk8();
funk9();
funk10();
funk11();
funk12();
funk13();
funk14();
funk15();
funk16();
funk17();
funk18();
funk19();
funk20();
funk21();
funk22();
funk23();
funk24();
//This is one way to do it
if (funk18() > funk1())
{
cout<<funk18<<" is the biggest";
}
//...
}

Here is a clean and elegant c++11 solution:
#include <iostream>
#include <functional>
#include <vector>
#include <limits>
#include <algorithm>
using namespace std;
using MyFunc = std::function<int()>;
int f1() { return 1; }
int f2() { return 15;}
int f3() { return 3; }
int main() {
std::vector<MyFunc> my_functions = {f1, f2, f3};
int max = std::numeric_limits<int>::min();
for (auto const &f : my_functions) {
max = std::max(max, f());
}
cout << max << endl;
return 0;
}
if you want to store the results from functions instead, you could do:
std::vector<int> my_results;
my_results.reserve(my_functions.size());
for (auto const &f : my_functions) {
my_results.push_back(f());
}
auto max_it = std::max_element(std::begin(my_results), std::end(my_results));
cout << *max_it << endl;

Related

How to pass parameters in an objects of array? in c++

class A
{
int id;
public:
A (int i) { id = i; }
void show() { cout << id << endl; }
};
int main()
{
A a[2];
a[0].show();
a[1].show();
return 0;
}
I get an error since there is no default constructor.However thats not my question.Is there a way that ı can send parameters when defining
A a[2];
A good practice is to declare your constructor explicit (unless it defines a conversion), especially if you have only one parameter. Than, you can create new objects and add them to your array, like this :
#include <iostream>
#include <string>
class A {
int id;
public:
explicit A (int i) { id = i; }
void show() { std::cout << id << std::endl; }
};
int main() {
A first(3);
A second(4);
A a[2] = {first, second};
a[0].show();
a[1].show();
return 0;
}
However, a better way is to use vectors (say in a week you want 4 objects in your array, or n object according to an input). You can do it like this:
#include <iostream>
#include <string>
#include <vector>
class A {
int id;
public:
explicit A (int i) { id = i; }
void show() { std::cout << id << std::endl; }
};
int main() {
std::vector<A> a;
int n = 0;
std::cin >> n;
for (int i = 0; i < n; i++) {
A temp(i); // or any other number you want your objects to initiate them.
a.push_back(temp);
a[i].show();
}
return 0;
}

Accessing functions of a class not working

I've just started learning C++ and am complete newbie, sorry in advance if the question will sound stupid
I have my program to solve Two Sum problem:
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> twoSum(const vector<int>& a, int target) {
unordered_map<int, int> valueToIndex;
for (int i = 0; i < (int)a.size(); i++) {
auto it = valueToIndex.find(target - a[i]);
if (it != valueToIndex.end()) {
return { it->second,i };
}
valueToIndex[a[i]] = i;
}
throw invalid_argument("sum not found");
}
};
int main()
{
vector<int> a{ 11,22,33,44,55 };
int target_value = 55;
Solution A;
A.twoSum(a,target_value);
return 0;
}
When I try to compile my program using test input values console returns nothing
What could be the issue?
Thanks!
Firstable, if you have a function that returns something, you need to get that return.
In your example, like that
vector<int> myResult = A.twoSum(a,target_value);
Then you can use that result like that.
for (const auto &value : myResult)
std::cout << value << std::endl;

C++ unique and set not working

unique is not working pls help
its showing compiler error
I tried making it a
set<str> se(ss.begin(), ss.end());
ss.assign(se.begin(), se.end());
I tried this too and it also shows compiler error
Is it because of the bool sortByString()
I saw that code in a page so that it helps to sort a vector class objects
if there is any other way pls help
#include <cmath>
#include<set>
#include <cstdio>
#include <vector>
#include <iostream>
#include<string>
#include <algorithm>
using namespace std;
int n;
class str
{
public:
string a;
void in(string s)
{
a=s;
}
string get(){
return a;
}
void out()
{
cout<<a;
}
};
bool sortByString(str &t1, str &t2)
{
return t1.get() < t2.get();
}
string d(vector<str> a)
{
string s;
for(int i=0;i<n;i++)
s.append(a[i].get());
return s;
}
int main() {
string s,sub;
cin >> s;
int length = s.length();
int i, k = 0, c;
vector<str> ss;
str a;
n = length*((length + 1) / 2);
k = 0;
for (c = 0; c < length; c++)
{
for (i =length-c;i>=1; i--)
{
a.in(s.substr(c,i));
ss.push_back(a);
}
}
s=" ";
ss.erase(unique(ss.begin(),ss.end()),ss.end()); /*code giving compiler error pls help*/
s=d(ss);
cout<<s;
return 0;
}
You are missing operator== in str. std::unique requires this operator.
class str
{
//....
bool operator==(const str& rop) const {
return a == rop.a;
}
};
bool sortByString(str &t1, str &t2)
bool sortByString(const str &t1, const str &t2)
At least for g++. VS will compile any, as I think.

This code shows error "stu undeclared"?? what should i do

I know this error is because i have declared stu inside the for loop scope but its the necessity of the program.I want to declare an array for each test case (test case should all be input at once).Suggest me a way to achieve this.Is dynamic memory an alternative.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
int n[t],g[t];
int m =0;
for(int w=0;w<t;t++)
{
cin>>n[w]>>g[w];
int stu[n[w]];
for(int i=0;i<n[w];i++)
{
cin>>stu[i];
}
}
while(m<t)
{
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]=1)
{
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]=n[m])
{
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++)
{
for(int k=(j+1);k<n[m];k++)
{
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
You are declaring stu inside of a for loop, so it is limited to the scope of the loop. You then try to use it outside of the loop, where it is undeclared.
for(int w=0;w<t;t++)
{
...
int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
// OK to use stu here
...
}
// stu doesn't exist here
Also note that standard C++ does not support variable length arrays (VLAs), which is what you are attempting to use in the declaration of stu, as well as here:
int t;
cin>>t;
int n[t],g[t];
You can replace these arrays by std::vector<int>:
#include <iostream>
#include <vector>
int main()
{
int t=0;
cin>>t;
std::vector<int> n(t);
std::vector<int> g(t);
std::vector<int> stu ...;
}
The line
int stu[n[w]];
is inside a block and outside that block it won't be seen. You should move it out of the block, but doing so of course you can't use n[w], being w the looping var. You coudl put a limit to the max value n[w] can have, e.g.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
int t;
cin>>t;
int n[t],g[t]; // <- supported by some compiler, but not std
int m =0;
int stu[MAXV];
for(int w=0;w<t;t++) {
cin>>n[w]>>g[w];
for(int i=0;i<n[w] && i < MAXV;i++) {
cin>>stu[i];
}
}
while(m<t) {
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]==1) {
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]==n[m]) {
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++) {
for(int k=(j+1);k<n[m];k++) {
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
(I've fixed a couple of assignment in conditional when I suppose you meant == and not =, but I've not tested if the code does what you expect: it just compile, with g++ but not with other compiler likely, see comment in code)

c++ reference to function pointer dynamically

I have one application in which following task are to be done
1.) UI application will send command code (integer value).
2.) DLL interface(in c++) will get that integer value and execute corresponding command function.
commands name and command code are maintained as
#define PING 50
there will be 500 commands and applying SWITCH CASE will not sound good. so i decided to implement function pointer in my code as below
#include "stdafx.h"
#include<iostream>
#define PING 20
using namespace std;
//extern const int PING = 10;
void ping()
{
cout<<"ping command executed";
}
void get_status(void)
{
cout<<"Get_status called"<<endl;
}
class ToDoCommands
{
public:
void getCommand( void (*CommandToCall)() );
};
void ToDoCommands::getCommand( void (*CommandToCall)())
{
void (*CommandToCall1)();
CommandToCall1 = CommandToCall;
CommandToCall1();
}
int main()
{
int code;
ToDoCommands obj;
cout<<"enter command code";
cin>>code; // if UI send 50 then Ping function get executed as #define PING 50
obj.getCommand(ping); // here m passing ping manually..
//obj.getCommand(get_status);
return 0;
}
how can i pass command name corresponding to command code in
obj.getCommand(ping);
You are almost there: make a std::map of std::string to function pointer, initialize it with data pairing a string name to a corresponding function pointer, and then use that map at runtime to pick the correct pointer based on the string parameter passed in.
#include <iostream>
#include <string>
#include <map>
using namespace std;
void ping() {
cout << "ping" << endl;
}
void test() {
cout << "test" << endl;
}
int main() {
map<string,void(*)()> m;
m["ping"] = ping;
m["test"] = test;
// I am using hard-coded constants below.
// In your case, strings will come from command line args
m["test"]();
m["ping"]();
return 0;
}
Link to a demo with std::map.
Here is how you can do it without a map (it will be slower because of the linear search, but you can fix it by ordering names alphabetically and using binary search).
#include <iostream>
#include <cstring>
using namespace std;
void ping() {
cout << "ping" << endl;
}
void test() {
cout << "test" << endl;
}
typedef void (*fptr_t)();
int main() {
const fptr_t fptrs[] = {test, ping};
const char *names[] = {"test", "ping"};
const char *fname = "test";
for (int i = 0 ; i != 2 ; i++) {
if (!strcmp(fname, names[i])) {
fptrs[i]();
break;
}
}
return 0;
}
Link to a demo with arrays.
Declare an array of function pointers. Where you treat the index as your "code". For example:
void foo(){
printf("foo\n");
}
void bar(){
printf("bar\n");
}
int main(void)
{
void (*code_to_function[100])();
int code;
code_to_function[0] = foo;
code_to_function[1] = bar;
printf("Enter code: ");
scanf("%d", &code);
code_to_function[code]();
return 0;
}
Please note that for this rudimentary example, inputting integer code other than 0 and 1 will result in a segfault.
I should say #dasblinkenlight is right but if you don't want to use std::map you should implement a map yourself. This can be buggy and not a optimized way, but if you don't want to use STL, it seems you should implement it yourself.
You can use 2 arrays with corresponding indices. One of them is a char * array and another one is function pointers. They are better to be encapsulated in a class named something like MyMap.
class MyMap {
public:
...
inline void add(char *name, (void (*ptr)(void)) ) {
names_[currIndex_] = name; // Or stcpy
ptrs_[currIndex_] = ptr;
currIndex_++;
}
inline (void(*)(void)) get(char *name) {
int foundIndex = -1;
for (int i = 0; i < currIndex_; i++) {
// Find matching index
}
if (foundIndex_ >= 0) {
return ptrs_[foundIndex_];
}
return NULL;
}
private:
int currIndex_;
char *names_[10];
(void (*ptrs_[10])(void));
};