我有一个关于c ++和数组的问题.
假设我有一个名为CustomArray的类,它只不过是具有大小和容量属性的通用数组,以使数组动态化.定义为:
template<typename T>
class CustomArray
{
public:
int capacity, size;
T* items;
//constructor
//destructor
//function1
//function2
//etc...
};
Run Code Online (Sandbox Code Playgroud)
现在我有点卡住,我想实现一个像以下的功能:"
void performOnAllItems(/*function?*/)
{
for(int i = 0; i < size; i++)
{
//perform function on element
}
}
Run Code Online (Sandbox Code Playgroud)
将另一个函数作为参数(如果可能的话?)并在所有元素上执行它.那可能吗?如果是的话......怎么样?
提前致谢.
我想在我的程序中编写一个实用程序,它可以将字符串转换为int.我知道我可以使用atoi或strtol,但我需要对它进行一些错误处理.哪种方式更好?我应该创建一个简单的全局函数,可能只在特定的命名空间中创建,还是创建一个具有可以为我执行此操作的成员的类?
例如:
namespace utility{
int ConvertStrToInt(std::string* str, int& convertednum)
{
//do the conversion and error handling
return convertednum;
}
}
Run Code Online (Sandbox Code Playgroud)
要么
class Utility{
public:
static int ConvertStrToInt(std::string* str, int& convertednum)
{//do the conversion and error handling here}
}
Run Code Online (Sandbox Code Playgroud)
对不起,如果这个问题听起来有点傻,但我和另外两个人在一个团队中,我们对此的看法截然不同.1表示该类适用于所有内容并为所有内容创建类,我认为对于这样一个简单的问题,一个简单的函数是一个很好的解决方案.
哪个更有效率?我什么时候应该使用一个简单的函数?什么时候从哪个类开始是一个好的解决方案?
感谢每个人!
我有一个函数,它将一个指向函数的参数作为参数:
void MySolver::set_pt2func(double(*pt2function)(double*))
{
pt2Function = pt2function;
}
Run Code Online (Sandbox Code Playgroud)
在另一个类中,某些方法想要使用以下代码初始化实例my_solver:
double (MyClass::*pt2apply)(double* parameter) = NULL;
pt2apply = &MyClass::apply_func;
my_solver->set_pt2func(pt2apply);
Run Code Online (Sandbox Code Playgroud)
最后一行显示编译错误:
error: double (MyClass::*pt2apply)(double*)
argument of type (MyClass::* )(double*) is not compatible
with argument of type double(*)(double*)
Run Code Online (Sandbox Code Playgroud)
怎么修?
谢谢!
编辑
我用这个小费
http://www.parashift.com/c++-faq/memfnptr-to-const-memfn.html
我设置了typedef:
typedef double (MyClass::*Pt2apply_func)(double*) const;
Run Code Online (Sandbox Code Playgroud)
在我的方法中,我试试这个:
Pt2apply_func pt2apply = &MyClass::apply_func;
my_solver->set_pt2func(pt2apply);
Run Code Online (Sandbox Code Playgroud)
和编译错误显示&MyClass,再次兼容类型.处理我的问题的其他方法?谢谢!
我按照这个/sf/answers/41300381/的答案写了下面的代码.
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;
struct alpha {
int x;
int y;
};
struct find_element
{
int y;
find_element(int y) : y(y) {}
bool operator ==( const alpha& l) const
{
return y == l.y;
}
};
int main() {
std::vector<alpha> vAlpha;
vAlpha[0].x = 10;
vAlpha[0].y = 100;
for(std::vector<alpha>::iterator it = vAlpha.begin(); it != vAlpha.end(); ++it) {
int k = 100;
// trying to find k in the complete vector
if …Run Code Online (Sandbox Code Playgroud) 函数关于函数的优点据说是它们保持状态.让我们说在我的问题中这不是一个相关的功能.
当我定义一个模板化的函数/类时,是否有任何规则可以选择将函数或函子作为模板参数更好?或者我可以基本上做同样的事情,所以这取决于我的口味?
(相关问题:Functor vs模板参数)
编辑:
我的问题部分回答(对于用户方)这里:使用这个无状态类与函数调用操作符vs ac样式函数的原因?
如何创建一组对,其中的元素(对)使用自定义bool函数进行排序?我写
set <pair<int,int>,compare> myset;
Run Code Online (Sandbox Code Playgroud)
并得到错误:参数2处的类型/值不匹配,预期类型,得到"比较"
我把"比较"定义为
bool compare(pair <int,int> g1, pair <int,int> g2)
{
return (g1.second-g1.first > g2.second-g2.first);
}
Run Code Online (Sandbox Code Playgroud)
而且当然
#include <vector>
#include <set>
Run Code Online (Sandbox Code Playgroud) 我已阅读以下问题:
我明白什么C++ functors是好的。但是我无法推断如果执行以下操作会发生什么:
template <typename T, unsigned int state>
class Foo {
public:
static Foo_func() { /* Do something */ };
}
// Partial specialization:
// -- state == 1
template <typename T>
class Foo <T, 1> {
public:
static Foo_func() { /* Do something */ };
}
template <typename F>
void call_func(F func) {
// Do something... //
func();
// Do something... //
}
int main() {
Foo <double, /*state*/ …Run Code Online (Sandbox Code Playgroud) 我是c ++模板的新手,所以请耐心等待.
我想要做的是通过使用模板化函数在我的类中实现某种策略模式.我认为这将内联策略.
我的理解是这可以通过仿函数来实现,但我不想引入新的类,我只想在我的类中内联策略函数.
让我们说我上课了Calculator.
Calculator.h
#ifndef CALCULATOR_H
#define CALCULATOR_H
class Calculator
{
public:
Calculator();
virtual ~Calculator();
typedef void (*Strategy)(int param1, int param2);
void add(int param1, int param2);
template<class T>
void doStrategy(T strategy, int param1, int param2);
protected:
private:
};
#endif
Run Code Online (Sandbox Code Playgroud)
Calculator.cpp
Calculator::Calculator()
{
//ctor
}
Calculator::~Calculator()
{
//dtor
}
void
Calculator::add(int param1, int param2)
{
std::cout << "Sum " << param1+param2 << std::endl;
}
template<class T>
void
Calculator::doStrategy(T strategy, int param1, int param2)
{
strategy(param1,param2);
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中 …
我正在使用以下版本,将无法使用 C++11 g++ (SUSE Linux) 4.3.4 [gcc-4_3-branch revision 152973]。
我有一个成对的向量。
std::vector<std::pair<int, std::string> > vec;
vec.push_back(std::make_pair(5, std::string("tata")));
vec.push_back(std::make_pair(6, std::string("tat2")));
vec.push_back(std::make_pair(7, std::string("tat3")));
vec.push_back(std::make_pair(8, std::string("tat4")));
Run Code Online (Sandbox Code Playgroud)
现在我可以使用迭代器使用该对的键搜索向量中的所有元素,例如
std::vector<std::pair<int, std::string> >:: iterator it ;
for (it = vec.begin(); it != vec.end(); ++it)
{
if (it->first == item)
{
cout << "Found " << item << "\n";
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我希望有任何可能的方法在 C++98 中使用 std::find 操作,因为我已经搜索了相关的帖子,其中大多数都解决了 C++ 11 支持的问题。
有人能解释一下这段代码的作用吗?
struct EnumClass
{
template <typename T>
std::size_t operator()(T t) const
{
return static_cast<std::size_t>(t);
}
};
Run Code Online (Sandbox Code Playgroud)