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

时间:2018-12-14 06:27:21

标签: c++ pointers vector struct

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

为此,我构建了一个类,使我可以将桌面引入虚幻引擎,但是在每个刻度上,它必须分配一种向量形式,该向量包含一个结构体来表示另一个数据类的值每次打勾时,我只想复制几个元素(像素颜色值)的内存地址,这样我就不必浪费时间去重复两次(对于台式机图像而言,它需要数百万次操作)

setTimeOut(function(){
    GetGovernerate()
},150,function(){
    setTimeOut(function(){
        GetDistrict();
    },150,function(){
        GetTown();
    });
});

奖金问题:

假设我有这个:

#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)


}

1 个答案:

答案 0 :(得分:2)

这应该产生您想要的输出,但是要注意。如评论中所述,如果您以任何方式更改<HEAD> <link rel="stylesheet" href="https://www.cafenocturne.com/IMVU/products/Shop_Code/ShopCSS.css"> </HEAD> <div id="container"> <div id="header" class="border shine"> <br><img src='' alt=''> [Header Image]<br><br> BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH </div> <div id="description" class="border shine"> <br>Description of product.<br><br> BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH </div> <div id="productimages" class="border shine"> <br><img src='' alt='Product Preview'> BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH BLAHBLAH </div> <div id="shine"> <div id="footer" class="border shine"> <!-- <a href="https://www.imvu.com/shop/web_search.php?manufacturers_id=5315966" target="_blank"> <img src="" border="0"></a> --> [Footer Banner]<br> </div> </div> </div>std::vector<int*>的大小并且对原始向量中的元素重新排序,则存储在vector1中的地址应被视为无效。 ,您的指针将指向错误的值。我还删除了vector2。参见:Why is “using namespace std” considered bad practice?

using namespace std;