Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何更改指向向量的指针的元素?_C++_Pointers_Vector_Struct - Fatal编程技术网

C++ 如何更改指向向量的指针的元素?

C++ 如何更改指向向量的指针的元素?,c++,pointers,vector,struct,C++,Pointers,Vector,Struct,假设我有两个整数向量和一个指向整数向量的指针。如何将指针的一个元素的值更改为其他整数向量中的地址 这方面的背景是,我已经构建了一个类,它允许我将桌面带入虚幻引擎,但是在每个勾号上,它必须为另一个数据类的值分配一个包含结构的向量形式。每个勾号,我只希望复制少数元素的内存地址(像素颜色值)因此,我不必为了拷贝两次而浪费时间(对于桌面映像来说,这需要数百万次操作) #包括 #包括 使用名称空间std; //打印功能 无效打印向量(向量v) { 对于(int i=0;i

假设我有两个整数向量和一个指向整数向量的指针。如何将指针的一个元素的值更改为其他整数向量中的地址

这方面的背景是,我已经构建了一个类,它允许我将桌面带入虚幻引擎,但是在每个勾号上,它必须为另一个数据类的值分配一个包含结构的向量形式。每个勾号,我只希望复制少数元素的内存地址(像素颜色值)因此,我不必为了拷贝两次而浪费时间(对于桌面映像来说,这需要数百万次操作)

#包括
#包括
使用名称空间std;
//打印功能
无效打印向量(向量v)
{
对于(int i=0;icout这应该会产生您想要的输出,但要注意。如注释中所述,如果您以任何方式更改
vector1
vector2
的大小,并且如果您重新排列原始向量中的元素,则指针将指向错误的位置,则存储在
std::vector
中的地址应被视为无效值。我还使用命名空间std;
删除了
。请参阅:

#包括
#包括
//打印功能
无效打印向量(标准::向量v){

对于(auto x:v)std::cout,通过指针寻址
std::vector
中的元素并非不可能,但我不建议这样做。请注意,一旦向向量添加元素,它可能会重新分配其内部存储。因此,所有指针都会断开(悬空)。更好的方法是通过索引对向量中的元素进行寻址。如果您坚持通过指针对向量元素进行寻址…您只需像往常一样使用地址运算符(
运算符(&)(
)即可获得向量元素的地址。例如,
std::vec={1,2,3,4,5};int*pA=&vec[2];//pA指向vec[2]现在。
可能是,我误解了您的意图。
ptrvector[0]=&vector2[2];
这将使用正确的间接寻址:
(*ptrvector)[0]=vector2[2]
请注意,指向向量的指针不是指针的向量。我相信你把这个弄混了…@Scheff谢谢!我不太清楚指针的正确符号。这正确地更新了指针的地址,但是如果我说assign
vector2[2]=someothernumber
向量2[2]的地址
我正试图避免的更改。这里可能会想到一些不可能的事情(我不知道它如何分配内存的全部细节)但其目的是,如果内存地址不变,我可以减少数百万次操作,因为指向向量的设置指针将更新地址处的值,而无需进行额外的复制。“然而,如果我说赋值
vector2[2]=someothernumber
vector2[2]
的地址将发生变化。”-不,它不改变地址
#include <iostream>
#include <vector>

using namespace std;

// Print function
void PrintVector(vector<int> v)
{
 for( int i = 0; i < v.size(); i++ )
 {
 cout << v[i] << ", ";
 }
 cout << endl;
}

int main()
{

    vector<int> vector1;
    vector<int> vector2;
    vector<int> *ptrvector;

    //Do some assignment so the vectors have values
    for( int i = 0; i<3; i++)
    {
        vector1.push_back(i);
        vector2.push_back(2*i);
    }
    //Assign the pointer to the address of vector1.
    ptrvector = &vector1;

    //Print out:
    PrintVector(vector1);  // (1,2,3)
    PrintVector(vector2);  // (2,4,6)
    PrintVector(*ptrvector); // (1,2,3)
    // We should see that lines 1 and 3 are the same

    //BROKEN BIT::

    //Ideally want something like
    ptrvector[0] = &vector2[2];
    PrintVector(*ptrvector); // (6,2,3);

    //Such that if I were to do this:
    vector2[2] = 20;
    PrintVector(*ptrvector); // It should change as a side effect: (20,2,3)


}
TArray<FColor> ColorData;
TArray<FColor> *ptrColorData
//Where TArray is essentially a vector. FColor is a struct with members (R,G,B,A)

//ColorData is initialised somewhere and we set the ptrColorData to the address
ptrColorData = &ColorData;

//Somewhere down the line we have a long loop whereby we do
ColorData[i].B = somedata[i];
ColorData[i].G = somedata[i+1];
ColorData[i].R = somedata[i+3];

//somedata is updated per tick, asigning ColorData per tick as well slows it down.
// I wish to be able to do something on the lines of this

ptrColorData[i].B = &somedata[i];
ptrColorData[i].G = &somedata[i+1];
ptrColorData[i].R = &somedata[i+3];

// this is only called once to initialize. Because the memory address
//is unchanging and somedata changes by functions on its own it means when
// I tell my unreal engine to update a texture by passing (*ptrColorData)
// to a function it automatically has the new data when that function is
// next called.
#include <iostream>
#include <vector>

// Print function
void PrintVector(std::vector<int> v) {
    for(auto x : v) std::cout << x << ", ";
    std::cout << "\n";
}

void PrintVector(std::vector<int*> v) {
    for(auto x : v) std::cout << *x << ", ";
    std::cout << "\n";
}

int main() {
    std::vector<int> vector1;
    std::vector<int> vector2;
    std::vector<int*> ptrvector; // pointers to the elements in vector1/2

    //Do some assignment so the vectors have values
    for( int i = 1; i<=3; i++) {
        vector1.push_back(i);
        vector2.push_back(2*i);
    }
    // Add pointers in ptrvector to the addresses in vector1.
    ptrvector.reserve(vector1.size());
    for(auto& r : vector1)
        ptrvector.emplace_back(&r);

    //Print out:
    PrintVector(vector1);   // (1,2,3)
    PrintVector(vector2);   // (2,4,6)
    PrintVector(ptrvector); // (1,2,3)

    ptrvector[0] = &vector2[2];
    PrintVector(ptrvector); // (6,2,3);

    vector2[2] = 20;
    PrintVector(ptrvector); // (20,2,3)
}