Tag: 向量

在C中使用char作为数组索引?

我有这个代码: int main(){ char vector[52]; char i; /* initialize the vector */ for (i =’a’; i < 'z'; i++){ vector[i] = i – 'a' + 1; } // vector is like vector['a'] = 1, vector['b'] = 2 .. vector['z'] = 26 for (i ='A'; i <= 'Z'; i++){ vector[i] = i – 'A' + 27; […]

C ++顺序读取多个输入文件

我有47个不同的文件: 001_template.dat … 047_template.dat 在名为/ data的目录中。 我需要将每个模板文件与三个不同的查询文件进行比较,也可以在目录中进行比较。 这些被命名为: 001_AU01_query.dat 001_AU12_query.dat 001_AU17_query.dat。 我知道如何让所有这些运行,但我将不得不将这6行代码剪切并粘贴46次,程序将变得非常长且令人困惑。 是否有一种循环这些文件的好方法? 可能通过循环模板文件,然后对每个模板进行三次查询? 我显然有一个相似性函数和一个已定义的排序函数,以及inputFile 。 这是我想要转换的代码:(不是作业,这是我一直在做的面部表情识别项目) int main() { vector temp01; vector temp12; vector temp17; temp01 = similar(inputFile(“data/001_AU01_query.dat”), inputFile(“data/001_template.dat”)); sortAndOutput(temp01); temp12 = similar(inputFile(“data/001_AU12_query.dat”), inputFile(“data/001_template.dat”)); sortAndOutput(temp12); temp17 = similar(inputFile(“data/001_AU17_query.dat”), inputFile(“data/001_template.dat”)); sortAndOutput(temp17); }

如何通过读取文件将不同数据类型的数据推入向量?

我想使用下面的代码将下面的数据推送到一个向量。但我的代码只适用于整数。我如何为以下数据执行此操作? 谢谢。 我的数据: M,0.455,0.365,0.095,0.514,0.2245,0.101,0.15,15 M,0.35,0.265,0.09,0.2255,0.0995,0.0485,0.07,7 F,0.53,0.42,0.135,0.677,0.2565,0.1415,0.21,9 M,0.44,0.365,0.125,0.516,0.2155,0.114,0.155,10 I,0.33,0.255,0.08,0.205,0.0895,0.0395,0.055,7 I,0.425,0.3,0.095,0.3515,0.141,0.0775,0.12,8 F,0.53,0.415,0.15,0.7775,0.237,0.1415,0.33,20 F,0.545,0.425,0.125,0.768,0.294,0.1495,0.26,16 M,0.475,0.37,0.125,0.5095,0.2165,0.1125,0.165,9 F,0.55,0.44,0.15,0.8945,0.3145,0.151,0.32,19 我的代码: fp = fopen(argv[1], “r”); //Opening the input file in read mode. if(!fp) { printf(“open data source file failed!\n”); goto MAINEXIT; } int ivalue; //extract data from files while(fscanf(fp,”%d,”,&ivalue)!=EOF) { printf(“Counter-%d\n”,counter++); srcdata.push_back(ivalue); //Pushing value by value into the vector with “,” delimiter. […]

将结构中的指针分配给变量

该程序应该创建一个动态内存向量。 我很确定我正确使用malloc。 我真正的问题是一些带指针的语法,特别是结构中的指针。 我正在尝试访问结构中的int指针的地址,所以我可以将它分配给另一个指针 我给出的结构是: typedef struct{ int *items; int capacity; int size; }VectorT; 而我正在努力工作的function是: int getVector(VectorT *v, int index){ int *p; p = v->items;//(2) p -= v->size; p += index; return *p; } 这应该取项目指针的地址减去列表中的项目数,并将所需项目的索引添加到p的地址。 然后我返回p的地址。 我有一种非常强烈的感觉,第(2)行不是我需要的语法。 根据我到目前为止的尝试,我的程序要么在调用getVector时崩溃,要么输出(我最好的猜测)一些内存位置。 这是添加向量的代码: void addVector(VectorT *v, int i){ if(v->size >= v->capacity){ //allocate twice as much as old vector and […]

2个2D向量的交叉积

任何人都可以提供返回TWO 2d向量的叉积的函数示例吗? 我正在尝试实现此算法 。 C代码会很棒。 谢谢。 编辑:发现另一种方式,它适用于2D,并且很容易。 bool tri2d::inTriangle(vec2d pt) { float AB = (pt.y-p1.y)*(p2.x-p1.x) – (pt.x-p1.x)*(p2.y-p1.y); float CA = (pt.y-p3.y)*(p1.x-p3.x) – (pt.x-p3.x)*(p1.y-p3.y); float BC = (pt.y-p2.y)*(p3.x-p2.x) – (pt.x-p2.x)*(p3.y-p2.y); if (AB*BC>0.f && BC*CA>0.f) return true; return false; }

在C中转换向量的C ++实现

我在C ++中编写了以下代码,但是发现我必须在C中转换它。我不是C甚至是C ++程序员,请帮忙。 有人可以帮助我将此方法更改为C指令,特别是向量实现,以下将无法编译我已删除复杂性以保持简单。 谢谢你的期待。 __declspec(dllexport) std::vector WINAPI ABC(char *strVal) { MY_STRUCT f; std::vector list = std::vector(); while (*dddd) { /*do the following for every feature in license file*/ f.attrib_num = fi.attrib_num; f.attrib_lic = fi.attrib_lic; list.push_back(f); } /* end while(conf) */ dddd++; printf(“\n”); } /* end while (*dddd) */ return flist; }

二进制矩阵向量乘法

我想将8×8 二进制矩阵乘以由无符号字符表示的8位向量表示为无符号64位整数。 但是,由于一些其他问题,矩阵必须按列排序,因此不容易匹配字节以便于乘法。 知道如何加快这样的计算吗? 每项操作都很重要,我需要进行数十亿次这样的计算。 乘法是在2元素场(F-2)上进行的。

基于小型c的矢量和矩阵库的建议

我需要一个轻量级的库,用于2D和3d矢量以及3×3和4×4矩阵。 在基本的C.这样我就不会重新发明轮子次优。 有什么建议?

用于迭代像数组一样的struct成员的C方法?

假设我有一个矢量类: typedef struct vec3_s { float x, y, z; } vec3; 但是,我希望能够迭代它而不将其转换为浮点数组。 虽然在这种情况下演员阵容是可以接受的,但我很想知道在C ++中是否有任何类似function的东西在C语言中是可行的。例如,在C ++中,因为std::vector具有subscript []运算符重载,我可以将其第一个索引的地址传递给一个带void*的函数void* 。 即 void do_something_with_pointer_to_mem( void* mem ) { // do stuff } int main( void ) { std::vector v; // fill v with values here // pass to do_something_with_pointer_to_mem do_some_with_pointer_to_mem( &v[ 0 ] ); return; } 另一个更具体的例子是在OpenGL中使用glBufferData(…)时(使用C ++时): glBufferData( […]

将数组映射回现有的特征矩阵

我想将double数组映射到现有的MatrixXd结构。 到目前为止,我已经设法将Eigen矩阵映射到一个简单的数组,但我找不到回来的方法。 void foo(MatrixXd matrix, int n){ double arrayd = new double[n*n]; // map the input matrix to an array Map(arrayd, n, n) = matrix; //do something with the array ……. // map array back to the existing matrix }