2013-03-12 23 views
0

我有這樣矢量的矢量替換:C++刪除&在載體

一個:0 1 0

B:1 0 1

C:0 1 1

我的程序的這部分工作。之後,我想刪除0,所以我使用的remove_if,我想更換1次由t和位置,所以我用replace_if爲終於有類似

一:T2

B:T1,T3

c:t2,t3

這2件事情在我的程序中不起作用,我不知道了。我認爲其中一個問題是我想在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; 
    } 

感謝

+1

請給我們一個_complete_例子。你的問題中的代碼缺少一些東西,比如'output'和't'。 – 2013-03-12 10:18:55

+0

你只是想在打印輸出中顯示**一個't',或者你想要一個元素的向量,其值實際上是「」t1「','」t2「'等。如果你想要這樣一個集合,那麼這些元素必須是字符串。整數是整數。 – 2013-03-12 10:22:10

回答

1

你讓一對夫婦的錯誤在那裏。如果您只想將一個領先的t打印到輸出中,下面我將展示如何重寫代碼的相關部分。注意,即使你真的想要一個容器的元素的值爲前導t(在這種情況下,它必須是容器的string s),移除元素的部分和替換元素的部分原始代碼不正確。

這裏是你應該如何改寫相關部分:

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

    // ... 
} 
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

+0

我不明白,如果我使用一個字符串我需要改變一切,不能使用替換和刪除? – usertfwr 2013-03-12 10:36:36

+0

主要變化將是比較操作。如果你在結果中需要't',你別無選擇,只能使用字符串。 – uba 2013-03-12 10:39:33