How to work with a vector array of struct? [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 4 years ago.
Improve this question
So I have this struct:
struct foo{
DWORD fooB;
char *fooA;
}
and I have a variable DWORD bar;, so how do I find if bar matches any fooB in my struct?
EDIT: my Code (currently)
#include <algorithm> // for. std::find
using namesapce std;
struct foo{
DWORD fooB;
char *fooA;
// .... Use this
}
vector <DWORD> foo;
if ( std::find(vector.begin(),
vector.end(), pIdToFind) !=
vector.end() )
// We found the item in the list, so let's just continue
else
// We haven't found it,

You could simply provide a comparison operator for comparing DWORDs to foos:
#include <vector>
#include <algorithm>
#include <windows.h>
struct foo {
DWORD fooB;
char *fooA;
};
bool operator==(DWORD lhs, foo const &rhs)
{
return lhs == rhs.fooB;
}
int main()
{
foo needle{ 42, nullptr };
vector<DWORD> haystack;
if (std::find(haystack.begin(), haystack.end(), needle) != haystack.end())
{
// We found the item in the list, so let's just continue
}
else
{
// not found
}
}

Related

How to move functions into a namespace? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 months ago.
Improve this question
The project calls functions from a library. I want to move those functions behind a namespace so that is easier to spot the places on the codebase where those functions are being called.
How functions are being called:
#include "foo.h"
int main()
{
foo();
bar();
return 0;
}
How I want to call them:
#include "myfoo.h"
int main()
{
thatlibrary::my_foo();
thatlibrary::my_bar();
return 0;
}
How I implemented that:
myfoo.h
namespace thatlibrary
{
void my_foo();
void my_bar();
}
myfoo.cpp
namespace thatlibrary
{
void my_foo()
{
foo();
}
void my_bar()
{
bar();
}
}
Wondering if there is any other solution? Perhaps more elegant.
Just use a using declaration in your namespace:
// global namespace
int foo() {
return 1;
}
namespace thatlibrary {
using ::foo;
}
auto i = thatlibrary::foo();
Notice that the name foo is still available in the global namespace, though.
// in global namespace
auto j = foo(); // works just fine
// from anywhere
auto k = ::foo(); // works just fine
I think the library is poorly designed, if it declares all its stuff in the global namespace.

create vector of array of string and void function pointer in 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 last year.
Improve this question
how to create an array or vetor like this in python:
this program in python:
a = [["foo",foofunc]["bar",barfunk]]
an array (or any thing) with another multi type array in,
I think you're looking for a vector of pair<std::string, void (*)() as shown below:
#include <iostream>
#include <utility>
#include <string>
#include <vector>
void foofunc()
{
std::cout<<"foofunc called"<<std::endl;
//do something here
}
void barfunc()
{
std::cout<<"barfunc called"<<std::endl;
//do something here
}
int main()
{
//create a vector of pair of std::string and pointer to function with no parameter and void return type
std::vector<std::pair<std::string, void (*)()>> a;
//add elements into the vector
a.push_back({"foo", &foofunc});
a.push_back({"bar", &barfunc});
//lets try calling the functions inside the vector of pairs
a[0].second();//calls foofunc()
a[1].second(); //calls barfunc()
return 0;
}
The output of the above program can be seen here.
You can take advantage of std::function as below:
#include <iostream>
#include <functional>
void foofunc( )
{
// your code goes here
std::clog << "Hi" << '\n';
}
int main( )
{
std::function< void( ) > f_foofunc = foofunc;
f_foofunc( ); // call it directly
std::vector< std::pair< std::string, std::function<void( )> > > func_vec;
func_vec.push_back( { "foo", &foofunc } );
func_vec[0].second( ); // call it from the vector
}

C++, Why can't I put the definition of class constructor with parameters-initialize list outside the class declaration [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Why can't I put the definition of class constructor with parameters-initialize list outside the class declaration?
typedef unsigned int UINT;
class num_sequence {
public:
typedef vector<UINT>::iterator iterator;
//I cannot put this following part in the cpp file
num_sequence(vector<UINT> & ele,int len=0,int beg=0):_relems(ele),_length(len),_beg_pos(beg)
{
//an abstract class cannot be instanlized
cout<<"build a num_sequence object";
}
virtual ~num_sequence();
num_sequence.h
#include <vector>
typedef unsigned int UINT;
class num_sequence
{
public:
typedef std::vector<UINT>::iterator iterator;
num_sequence(std::vector<UINT> & ele, int len = 0, int beg = 0);
virtual ~num_sequence();
private:
std::vector<UINT> &_relems;
int _length;
int _beg_pos;
};
num_sequence.cpp
#include "num_sequence.h"
#include <iostream>
num_sequence::num_sequence(std::vector<UINT> & ele, int len, int beg)
: _relems(ele), _length(len), _beg_pos(beg)
{
std::cout << "build a num_sequence object";
}
num_sequence::~num_sequence()
{
}

C++ error: "size" declared as function returning a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm not sure why I am getting an error during compilation that says "error: "size" declared as function returning a function" when size() is returning a type size_t. Any help would be appreciated, thanks.
// Text.h
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
namespace w3 {
class Text {
string* arrayRecords;
size_t numRecords;
public:
Text();
Text(const char* fileName);
size_t size() const;
~Text();
};
}
// Text.cpp
#include "Text.h"
namespace w3 {
Text::Text() {
numRecords = 0;
arrayRecords = nullptr;
}
Text::Text(const char* fileName) {
//
}
size_t Text::size() const() {
return numRecords;
}
Text::~Text() {
if(arrayRecords)
delete [] arrayRecords;
}
}
The problem is with this line:
size_t Text::size() const()
Remove the () after const, so you have:
size_t Text::size() const

Create a variable inside another statement [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 7 years ago.
Improve this question
Is there a way to allocate a variable within the scope of a parameter list? By using new we can do the following :
Class A{ /*... snip ...*/ };
void myFunc(A* a){ }
int main(...){
myFunc(new A());
return 0;
}
This will create a new A. What if the signature of myFunc was
void myFunc(A a);
instead. Is there a syntax to create local instance inside the myFunc() parameter list? I'm looking for something like
myFunc(A());
or
myFunc(A a());
Another use would be for something like :
A a(123);
if(a == A(123)){ }
The net effect is to save one line, but it also creates a scope within the parameters list which makes me wonder if it is allowed at all.
If you just want to create a variable to pass to the function you can use a aggregate initialization / list initialization
#include <iostream>
#include <cmath>
using namespace std;
class A{ /*... snip ...*/ };
void myFunc(A a){ }
int main(){
myFunc(A{});
return 0;
}
Live Example
You can also use this with classes that have constructors that take multiple parameters
#include <iostream>
#include <cmath>
using namespace std;
class A
{
private:
int foo;
int bar;
double foobar;
public:
A(int a, int b, double c) : foo(a), bar(b), foobar(c) {}
};
void myFunc(A a){ }
int main(){
myFunc(A{1,2,3.0});
return 0;
}
Live Example
C++ supports this with the myFunc(A()); syntax you posed in your question.
#include <stdio.h>
char lazybuff[500];
class Point
{
public:
Point (double x, double y) : m_x(x), m_y(y) { }
char * ToString (void) { sprintf (lazybuff, "%f, %f", m_x, m_y); return lazybuff; }
private:
double m_x, m_y;
};
void print_point (Point print_me)
{
printf ("%s\n", print_me.ToString());
}
int main (void)
{
print_point (Point(5, 3));
return 0;
}