指向其他向量元素的指针的向量

发布于 2024-11-07 08:37:19 字数 1131 浏览 0 评论 0原文

EI 有一个函数,它接受向量的参数指针:

void Function(std::vector<type>* aa)

现在在这个函数中,我想将数据从该向量过滤到另一个向量,并且我想通过改变这个临时向量的值来改变原始向量的数据。该死的,很难理解这样的事情:

void Function(std::vector<type>* aa)
{
    std::vector<type*> temp; //to this vector I filter out data and by changning 
    //values of this vector I want to autmatically change values of aa vector
}

我有这样的事情:

void Announce_Event(std::vector<Event>& foo)
{
    std::vector<Event> current;
    tm current_time = {0,0,0,0,0,0,0,0,0};
    time_t thetime;
    thetime = time(NULL);
    localtime_s(&current_time, &thetime);
    for (unsigned i = 0; i < foo.size(); ++i) {
        if (foo[i].day == current_time.tm_mday &&
            foo[i].month == current_time.tm_mon &&
            foo[i].year == current_time.tm_year+1900)
        {
            current.push_back(foo[i]);
        }
    }
    std::cout << current.size() << std::endl;
    current[0].title = "Changed"; //<-- this is suppose to change value.
}

这不会改变原始价值。

EI have function which takes as parameter pointer to vector:

void Function(std::vector<type>* aa)

Now inside this function I want to filter out data from that vector to another vector and I want to change data of original vector by changing values of this temporary one. Damn it's hard to understand something like:

void Function(std::vector<type>* aa)
{
    std::vector<type*> temp; //to this vector I filter out data and by changning 
    //values of this vector I want to autmatically change values of aa vector
}

I have something like that:

void Announce_Event(std::vector<Event>& foo)
{
    std::vector<Event> current;
    tm current_time = {0,0,0,0,0,0,0,0,0};
    time_t thetime;
    thetime = time(NULL);
    localtime_s(¤t_time, &thetime);
    for (unsigned i = 0; i < foo.size(); ++i) {
        if (foo[i].day == current_time.tm_mday &&
            foo[i].month == current_time.tm_mon &&
            foo[i].year == current_time.tm_year+1900)
        {
            current.push_back(foo[i]);
        }
    }
    std::cout << current.size() << std::endl;
    current[0].title = "Changed"; //<-- this is suppose to change value.
}

That does not change original value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拒绝两难 2024-11-14 08:37:19

我认为你可能在表达你的意图时遇到困难,所以这需要一个心灵的答案。

void Func(std::vector<type> & aa)
{
    std::vector<type*> temp;

    // I wish <algorithm> had a 'transform_if'    
    for(int i=0; i<aa.size(); ++i)
    {
        if( some_test(aa[i]) )
            temp.push_back(&aa[i])
    }

    // This leaves temp with pointers to some of the elements of aa.
    // Only those elements which passed some_test().  Now any modifications
    // to the dereferenced pointers in temp will modify those elements
    // of aa.  However, keep in mind that if elements are added or
    // removed from aa, it may invalidate the pointers in temp.
}

I think you may be having trouble communicating your intentions, so this calls for a psychic answer.

void Func(std::vector<type> & aa)
{
    std::vector<type*> temp;

    // I wish <algorithm> had a 'transform_if'    
    for(int i=0; i<aa.size(); ++i)
    {
        if( some_test(aa[i]) )
            temp.push_back(&aa[i])
    }

    // This leaves temp with pointers to some of the elements of aa.
    // Only those elements which passed some_test().  Now any modifications
    // to the dereferenced pointers in temp will modify those elements
    // of aa.  However, keep in mind that if elements are added or
    // removed from aa, it may invalidate the pointers in temp.
}
你是年少的欢喜 2024-11-14 08:37:19

不要使用指向向量的指针,而是使用引用:

void Function(std::vector<type>& aa)

在函数内,您现在可以照常访问向量内容。

void Function(std::vector<type>& aa)
{
    std::vector<type>& temp = aa;

    // if you now append something to temp, it is also appended to aa
    aa.push_back(type());
}

我不知道为什么你想要对一个向量进行两个引用,但是嘿,你问了:)

编辑:删除了拼写错误,请参阅评论。谢谢

Do not use a pointer to a vector, use a reference instead:

void Function(std::vector<type>& aa)

inside the function you can now access the vectors contents as usual.

void Function(std::vector<type>& aa)
{
    std::vector<type>& temp = aa;

    // if you now append something to temp, it is also appended to aa
    aa.push_back(type());
}

I don't know why you want two references to one vector, but hey, you asked :)

EDIT: removed typo, see comments. thanx

離人涙 2024-11-14 08:37:19

顺便说一句,开始更好地格式化你的代码。凌乱的代码很难理解,并且让你更难弄清楚你想要做什么。

这将执行您想要的操作:

void Oglos_Wydarzenie(std::vector<Wydarzenie>& zmienna)
{
    std::vector<Wydarzenie *> obecne;
    tm AktualnyCzas = {0,0,0,0,0,0,0,0,0};
    time_t czas;
    czas = time(NULL);
    localtime_s(&AktualnyCzas,&czas);
    for (unsigned i = 0; i < zmienna.size(); ++i) {
        if (zmienna[i].dzien == AktualnyCzas.tm_mday &&
            zmienna[i].miesiac ==  AktualnyCzas.tm_mon &&
            zmienna[i].rok == AktualnyCzas.tm_year+1900)
        {
            obecne.push_back(&zmienna[i]);
        }
    }
    std::cout << obecne.size() << std::endl;
    obecne[0]->tytul = "Changed"; //<-- this is suppose to change value.
}

您可以使用所有指针而不使用任何引用来执行此操作,但它看起来更令人困惑:

void Oglos_Wydarzenie(std::vector<Wydarzenie>* zmienna)
{
    std::vector<Wydarzenie *> obecne;
    tm AktualnyCzas = {0,0,0,0,0,0,0,0,0};
    time_t czas;
    czas = time(NULL);
    localtime_s(&AktualnyCzas,&czas);
    for (unsigned i = 0; i < zmienna->size(); ++i) {
        if ((*zmienna)[i].dzien == AktualnyCzas.tm_mday &&
            (*zmienna)[i].miesiac ==  AktualnyCzas.tm_mon &&
            (*zmienna)[i].rok == AktualnyCzas.tm_year+1900)
        {
            obecne.push_back(&((*zmienna)[i]));
        }
    }
    std::cout << obecne.size() << std::endl;
    obecne[0]->tytul = "Changed"; //<-- this is suppose to change value.
}

As an aside, start formatting your code better. Messy code is difficult to understand and makes it harder for you to figure out what you're trying to do.

This will do what you want:

void Oglos_Wydarzenie(std::vector<Wydarzenie>& zmienna)
{
    std::vector<Wydarzenie *> obecne;
    tm AktualnyCzas = {0,0,0,0,0,0,0,0,0};
    time_t czas;
    czas = time(NULL);
    localtime_s(&AktualnyCzas,&czas);
    for (unsigned i = 0; i < zmienna.size(); ++i) {
        if (zmienna[i].dzien == AktualnyCzas.tm_mday &&
            zmienna[i].miesiac ==  AktualnyCzas.tm_mon &&
            zmienna[i].rok == AktualnyCzas.tm_year+1900)
        {
            obecne.push_back(&zmienna[i]);
        }
    }
    std::cout << obecne.size() << std::endl;
    obecne[0]->tytul = "Changed"; //<-- this is suppose to change value.
}

You could do this with all pointers and no references at all, but then it looks much more confusing:

void Oglos_Wydarzenie(std::vector<Wydarzenie>* zmienna)
{
    std::vector<Wydarzenie *> obecne;
    tm AktualnyCzas = {0,0,0,0,0,0,0,0,0};
    time_t czas;
    czas = time(NULL);
    localtime_s(&AktualnyCzas,&czas);
    for (unsigned i = 0; i < zmienna->size(); ++i) {
        if ((*zmienna)[i].dzien == AktualnyCzas.tm_mday &&
            (*zmienna)[i].miesiac ==  AktualnyCzas.tm_mon &&
            (*zmienna)[i].rok == AktualnyCzas.tm_year+1900)
        {
            obecne.push_back(&((*zmienna)[i]));
        }
    }
    std::cout << obecne.size() << std::endl;
    obecne[0]->tytul = "Changed"; //<-- this is suppose to change value.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文