0

我有一个这样的向量向量:

一个:0 1 0

乙:1 0 1

c: 0 1 1

我的程序的这一部分有效。在我想删除 0 所以我使用 remove_if 并且我想用 t 和位置替换 1 之后,所以我使用 replace_if 最终有类似的东西

一个:t2

b: t1, t3

c: t2, t3

这两件事在我的程序上不起作用,我也不知道了。我认为问题之一是我想在 int 中有“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;
    }

谢谢

4

2 回答 2

1

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

以下是您应该如何重写相关部分:

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

    // ...
}
于 2013-03-12T10:37:36.850 回答
0

使用字符串向量的向量而不是 int,因为您需要字符串值,例如 t1。即替换

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

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

要分配字符串“0”和“1”,您可以替换 newVector[i] = (rand() % 2); 经过

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

您将需要使用字符串比较运算符std::string::compare更改其余的整数比较

于 2013-03-12T10:23:06.660 回答