我已经设法绕过一些C++的功能(for_each,映射函数,使用迭代器......)但是用于接收泛型容器和迭代器的模板和函数参数列表的构造仍然无法实现.我有一个实际的例子,我希望有人可以为我说明:
使用以下函数处理传入的std :: vector并构建一个进程的许多数据点/迭代的运行总计:
/* the for-loop method - not very savvy */
void UpdateRunningTotal (int_vec& total, int_vec& data_point) {
for (int i = 0; i < V_SIZE; i++) {
total[i] += data_point[i];
}
}
typedef int_vec std::vector<int>;
int_vec running_total (V_SIZE, 0); // create a container to hold all the "data points" over many iterations
/* further initialization, and some elaborate loop to create data points */
UpdateRunningTotal (running_total, iteration_data);
/* further processing */
Run Code Online (Sandbox Code Playgroud)
上面的工作,但我宁愿有一个函数,它接受迭代器并执行此求和.更好的是,使用推导类型的通用参数列表而不是指定容器类型,即:
UpdateRunningTotal (iteration_data.begin(), iteration_data.end(), running_total.begin());
Run Code Online (Sandbox Code Playgroud)
我现在真的迷失了,需要一些指导来找到如何定义模板和参数列表以使函数通用.模板和函数定义是什么样的?我已经熟悉使用STL功能执行此特定任务的方法 …
我遇到了解CLOS处理类中文件访问的方式的问题.在c ++中,我可以这样做:
class Foo {
Foo (string filename); // opens the file (my_file) requested by the filename
~Foo (); // close the file
FILE * my_file; // a persistent file-handle
DataStruct my_data; // some data
void ParseData (); // will perform some function on the file and populate my_data
DataStruct * GetData () { return &my_data; } // accessor to the data
};
Run Code Online (Sandbox Code Playgroud)
我想指出的是PraseData()将被多次调用,并且每次从文件中解析出新的数据块并且my_data将被更改.
我正在尝试在CLOS中执行相同的技巧 - 创建所有通用方法来解析数据,加载文件,读取标题等,以及我所拥有的类定义:
(defclass data-file ()
((filename :initarg :filename :accessor filename)
(file :accessor file)
(frame :accessor …Run Code Online (Sandbox Code Playgroud) 我一直试图说服我的朋友避免使用动态分配的数组并开始转移到STL向量.我给他发了一些示例代码来展示可以用STL和仿函数/生成器完成的一些事情:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#define EVENTS 10000000
struct random_double {
double operator() () { return (double)rand()/RAND_MAX; }
};
int main(int argc, char **argv){
std::vector<double> vd (EVENTS);
generate(vd.begin(), vd.end(), random_double());
copy(vd.begin(), vd.end(), std::ostream_iterator<double>(std::cout, "\n"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
他对此的回答,虽然他觉得它更优雅,但是他自己的代码更快(几乎是2倍!)这是他回答的C代码:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define EVENTS 10000000
__inline double random_double() {
return (double)rand()/RAND_MAX;
}
int main(int argc, char **argv){
unsigned int i;
double *vd;
vd = (double *) malloc(EVENTS*sizeof(double));
for(i=0;i<EVENTS;i++){ vd[i]=random_double(); }
for(i=0;i<EVENTS;i++){ printf("%lf\n",vd[i]); …Run Code Online (Sandbox Code Playgroud) 我试图编写一个简短的实用程序函数,它接受两个std :: pair项并测试它们的相等性,但忽略了元素的排序.另外(这是我遇到麻烦的地方)我写了一个函数来获取那些std :: pair项的容器并测试容器中给定对参数的成员资格.
/* A quick functor way to check the identity of the two items of a pair to see if each pair contains the same items regardless of order */
template <class T>
class EqualPairs : public std::binary_function<T,T,bool> {
T arg2;
public:
explicit EqualPairs (const T& x) : arg2(x) { }
bool operator() (const T& arg1) {
bool same = false;
if (arg1 == arg2 || (arg1.first == arg2.second && arg1.second == arg2.first))
same = true; …Run Code Online (Sandbox Code Playgroud) c++ ×3
iterator ×2
stl ×2
class-design ×1
clos ×1
common-lisp ×1
containers ×1
functor ×1
io ×1
lisp ×1
optimization ×1