小编use*_*281的帖子

数组等于另一个数组

我有一个这样的循环,其中arrayfunction设置所有数组值,compute_with_both_arrays根据这两个数组计算一个数字.

他们我在下面做的方式不适用于array1 = array2.有没有办法,我可以做到这一点,而不是在每个循环中运行arrayfuncion两次?

float sum = 0;

float array1[10];
arrayfunction(0, array1);

for(i=1; i<10; i++) {
  float array2[10]
  arrayfunction(1, array2);

  float s;
  s = compute_with_both_arrays(array1, array2);
  sum = sum + s;

  array1 = array2;
}
Run Code Online (Sandbox Code Playgroud)

c arrays pointers

9
推荐指数
1
解决办法
3万
查看次数

使用PInvoke返回从C到C#的点(x,y,z)列表

我需要使用PInvoke将我从C dll中获得的点列表返回给C#应用程序.这些是3维[x,y,z]中的点.点数因型号而异.在C i中处理这个结构的链表.但我不知道如何将其传递给C#.

我看到它的方式,我必须返回一个灵活的二维数组,可能在一个结构中.

有关如何做到这一点的任何建议?关于如何在C中返回它以及如何在C#中访问它的想法都受到高度赞赏.

c c# pinvoke

7
推荐指数
1
解决办法
997
查看次数

多边形的三角剖分

我试图对多边形进行三角测量,以便在3d模型中使用.当我尝试在多边形上使用耳朵方法,点下面点,我得到红线所在的三角形.由于这些三角形内没有其他点,这可能是正确的.但我希望它只对黑线内的区域进行三角测量.有人知道会有这样的算法吗?

在此输入图像描述

c geometry triangulation

6
推荐指数
1
解决办法
3479
查看次数

查找对象浮动状态的优化方法

要解决的问题是找到浮体的浮动状态,考虑其重量和重心.

我使用的功能计算给定下沉,鞋跟和修剪的身体的移位体积和中心.下沉是长度单位,后跟/修剪的角度限制在-90到90之间.

三个变量的插图

当移位的体积等于重量并且重心处于具有中心的垂直线时发现浮动状态.

我将此实现为具有3个变量(下沉,修剪,后跟)和3个方程的非线性Newton-Raphson根发现问题.这种方法有效,但需要良好的初步猜测.所以我希望找到一个更好的方法,或者找到初始值的好方法.

下面是用于Newton-Raphson迭代的牛顿和雅可比算法的代码.功能体积采用参数下沉,鞋跟和修剪.并返回体积,以及bouyancy中心的坐标.

我还包括了maxabs和GSolve2算法,我相信这些算法来自Numerical Recipies.

void jacobian(float x[], float weight, float vcg, float tcg, float lcg, float jac[][3], float f0[]) {
    float h = 0.0001f;
    float temp;
    float j_volume, j_vcb, j_lcb, j_tcb;
    float f1[3];

    volume(x[0], x[1], x[2], j_volume, j_lcb, j_vcb, j_tcb);
    f0[0] = j_volume-weight;
    f0[1] = j_tcb-tcg;
    f0[2] = j_lcb-lcg;

    for (int i=0;i<3;i++) {
        temp = x[i];
        x[i] = temp + h;
        volume(x[0], x[1], x[2], j_volume, j_lcb, j_vcb, j_tcb);

        f1[0] = j_volume-weight;
        f1[1] = j_tcb-tcg;
        f1[2] = j_lcb-lcg; …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm math mathematical-optimization nonlinear-optimization

5
推荐指数
1
解决办法
502
查看次数

使用 PInvoke 返回包含 char[] 结构的结构数组

我有一个从 PInvoke 返回的结构数组,如果结构只包含 int 或 float,它返回数组很好,但是当我尝试返回一个 char 数组时,它开始变得混乱,我尝试返回一个 IntPtr,但是那没有成功。我有什么想法可以让这个工作吗?

代码

struct  return_part {
    int partid;
    int numcomp;
    int parttype;
    char partname[100];
};

extern int return_parts(return_part ** array, int * arraySizeInElements) {
    int partcount = 0;
    struct list_part *currentnode; 
    currentnode = head;
    struct section_list *section;
    struct return_part *temppart;

    while (currentnode != NULL) {
        partcount++;
        currentnode = currentnode->next;
    }

    currentnode = head;
    *arraySizeInElements = partcount;

    int bytesToAlloc = sizeof(return_part) * (*arraySizeInElements);
    return_part * a = static_cast<return_part *>(CoTaskMemAlloc(bytesToAlloc));
    *array = a;

    int q …
Run Code Online (Sandbox Code Playgroud)

c# pinvoke

3
推荐指数
1
解决办法
3049
查看次数

改善并行计算的内存布局

我正在尝试使用C++ AMP优化用于并行计算的算法(Lattice Boltzmann).并寻找一些优化内存布局的建议,发现从结构中删除一个参数到另一个向量(被阻止的向量)给出并增加了大约10%.

任何人都有任何可以进一步改善这一点的提示,或者我应该考虑的事项?下面是每个时间步执行的最耗时的函数,以及用于布局的结构.

struct grid_cell {
//  int blocked;    // Define if blocked
    float n;        // North
    float ne;       // North-East
    float e;        // East
    float se;       // South-East
    float s;
    float sw;
    float w;
    float nw;
    float c;        // Center
};

int collision(const struct st_parameters param, vector<struct grid_cell> &node, vector<struct grid_cell> &tmp_node, vector<int> &obstacle) {
    int x,y;
    int i = 0;
    float c_sq = 1.0f/3.0f;     // Square of speed of sound
    float w0 = 4.0f/9.0f;       // Weighting factors …
Run Code Online (Sandbox Code Playgroud)

c++ parallel-processing gpgpu c++-amp

2
推荐指数
2
解决办法
433
查看次数

结构链接到链表

我是c ++的新手,我正在尝试将我在python中创建的程序移植到c ++.我有一个结构,它是一个包含部件列表的链表.这些部件中的每一个都包含一个或多个部件.所以我试图创建两个结构,其中一个结构链接到另一个结构.

但我似乎没有得到list_part链接到component_list.

struct list_part {
    char partname[100];
    int parttype;
    component_list * comp;
    list_part * next;
};

struct component_list {
    char compname[100];
    component_list * next;
};
Run Code Online (Sandbox Code Playgroud)

我使用以下函数将部件添加到列表的底部.

void addpart(char partname[], int parttype, component_list *newcomp) {
    struct list_part *temppart;
    struct list_part *currentpart;

    temppart = (struct list_part *)malloc(sizeof(struct list_part));
    strcpy_s(temppart->partname,partname);
    temppart->parttype = parttype;
    temppart->comp = newcomp;

    currentpart = head;

    if (head == NULL) {
        head = temppart;
        head->next = NULL;
    } else {
        while (currentpart->next != NULL) {
            currentpart = currentpart->next; …
Run Code Online (Sandbox Code Playgroud)

c++ data-structures

1
推荐指数
1
解决办法
236
查看次数

c风格链表和c ++ std :: vector的速度差异很大

从我读过的内容以及我理解内存结构如何工作的方式来看,向量创建了一个数组,其中所有的值都相互对齐,而我在下面放在一起的链表将指向内存中的随机点,其中下一个对象被分配.因此,该代码的向量应该更快.

当我编译下面的代码时,链接列表的运行时间为4秒,而向量在21秒内运行,这是向量的5倍速度增加.

我在这里错过了什么吗?或者有人对此有解释吗?

#include <vector>
#include <iostream>
#include <ctime>

using namespace std;

struct point{
    float t;
    float v;
};

struct pointlist{
    float t;
    float v;
    struct pointlist *next;
};

void fillpoints(std::vector<struct point> &points, struct pointlist *pointlist) {
    int i;

    for (i = 0; i<8;i++) {
        struct point newpoint; // Fill vector
        newpoint.t = (float)i;
        newpoint.v = (float)i/2;
        points.push_back(newpoint);

        pointlist->t = (float)i; // Fill linked list
        pointlist->v = (float)i/2;
        struct pointlist *temppoint = (struct pointlist *)malloc(sizeof(struct pointlist));
        pointlist->next = temppoint;
        pointlist …
Run Code Online (Sandbox Code Playgroud)

c++ stl

1
推荐指数
1
解决办法
411
查看次数

在函数内访问类似数组[]的向量

我想在"操纵向量"函数中访问向量,方法与访问带有vector [i]的数组相同,而不是在下面的代码中的(i)处使用vector->.我试图直接传递矢量,而不是像数组那样的指针.但这似乎破坏了该计划.任何想法如何实现这一目标?我是使用std库的新手,因为我主要有C的经验.

#include <vector>
#include <iostream>
#define vectorsize 5
struct st_test {
    int ii;
    float dd;
};

void manipulatevector(std::vector<struct st_test> *test) {
    test->resize(vectorsize);

    for(int i=0;i<vectorsize;i++) {
        test->at(i).dd = i*0.4f;
        test->at(i).ii = i;
    }
}

void manipulatearray(struct st_test test[vectorsize]) {
    for(int i=0;i<vectorsize;i++) {
        test[i].dd = i*0.4f;
        test[i].ii = i;
    }
}

void main() {
    std::vector<struct st_test> test1;
    manipulatevector(&test1);

    struct st_test test2[vectorsize];
    manipulatearray(test2);

    std::cout << "Vector" << std::endl;
    for(int i=0;i<vectorsize;i++) {
        std::cout << test1.at(i).dd << std::endl;
    }

    std::cout << "Array" << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ vector std

0
推荐指数
1
解决办法
6864
查看次数