C++ 在矢量中删除和替换

C++ remove & replace in a vector

本文关键字:删除 替换 C++      更新时间:2023-10-16

我有一个向量的向量:

a: 0 1 0

b: 1 0 1

c: 0 1 1

我的程序的这一部分工作。在我想要删除0之后我使用remove_if我想要用t和位置替换1所以我使用replace_if来最终得到像

这样的东西

: t2

b: t1, t3

c: t2, t3

这两件事在我的程序中不起作用,我再也没有主意了。我想其中一个问题是,我想把"t"放在整型中,但我不知道是否可以使用字符串。

下面是我的代码:
#include <iostream>
#include <vector>
#include <iterator>
     int main (){
    srand(time(0));
    int e = (rand() % 10) + 3;
    int tim = (rand() % 10) +1 ;
    std::vector< std::vector<int> > myvector;
    std::cout << "Time: 0 = Not available, 1 = available" << std::endl;
    for(int vec = 0; vec < e; vec++){
            std::vector<int> newVector(tim);
            for(int i = 0; i < tim; i++){
                    newVector[i] = (rand() % 2);
            }
            myvector.push_back(newVector);
            std::cout << "The Time " << (vec+1) << " is ";
            for(int b = 0; b < tim; b++){
                    int value = myvector[vec][b];
                    std::cout << " " << value << " ";
            }
    }
    //Delete the 0 in time
    int int_to_remove = 0;
    remove_if(myvector.begin(), myvector.end(), int_to_remove);
    std::cout << "The new Time "<< std::endl;
    copy(myvector.begin(), myvector.end(), output);
    //Change the name of 1 to t1
    int int_to_replace = 1;
    replace_if(myvector.begin(), myvector.end(), int_to_replace, t(tim));
    std::cout << "The new Time"<< std::endl;
    copy(myvector.begin(), myvector.end(), output);
    return 0;
    }

谢谢

你在上面犯了几个错误。如果您只希望将前导t打印到输出中,下面我将展示如何重写代码的相关部分。请注意,即使您确实想要一个值以t为前导的元素的容器(在这种情况下,它必须是string的容器),原始代码中删除元素的部分和替换元素的部分也是不正确的。

以下是你应该如何重写相关部分:
#include <algorithm>   // For copy(), replace(), erase(), and for_each()
#include <iterator>    // For ostream_iterator
// ...
int main () {
// ...
    //Delete the 0 in time
    int int_to_remove = 0;
    std::cout << "The new Time "<< std::endl;
    for_each(begin(myvector), end(myvector), [=] (std::vector<int>& v)
    {
        v.erase(remove(begin(v), end(v), int_to_remove), end(v));
    //  ^^^^^^^^^^^^^^                                 ^^^^^^^^
    //  If you don't do the above, your container won't be resized,
    //  and will have trailing "dead" elements. Moreover, if you
    //  use remove_if() with a predicate which is always equal to 0,
    //  you won't remove any element.
        copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, " "));
    //                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //                         This is how you can copy each element to the
    //                         standard output.
        std::cout << std::endl;
    });
    //Change the name of 1 to t1
    int int_to_replace = 1;
    std::cout << "The new Time "<< std::endl;
    for_each(begin(myvector), end(myvector), [=] (std::vector<int>& v)
    {
        // This does the replacement. Again, using replace_if() is wrong,
        // because you are not passing in a predicate, but a value.
        replace(begin(v), end(v), int_to_replace, tim);
        // This prints each element of the vector with a leading 't'
        for_each(begin(v), end(v), [] (int i)
        {
            std::cout << "t" << i << " ";
        });
        std::cout << std::endl;
    });
    // ...
}

使用vector of vector of strings而不是int,因为您需要像t1这样的字符串值。即替换

std::vector< std::vector<int> > myvector;

std::vector< std::vector<std::string> > myvector;

要分配字符串"0"answers"1",您可以替换newVector[i] = (rand() % 2);

if(rand() % 2)
  newVector[i] = "1";
else
  newVector[i] = "0";

您需要使用字符串比较操作符std::string::compare

更改其余的整数比较。