我有這樣矢量的矢量替換: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;
}
感謝
請給我們一個_complete_例子。你的問題中的代碼缺少一些東西,比如'output'和't'。 – 2013-03-12 10:18:55
你只是想在打印輸出中顯示**一個't',或者你想要一個元素的向量,其值實際上是「」t1「','」t2「'等。如果你想要這樣一個集合,那麼這些元素必須是字符串。整數是整數。 – 2013-03-12 10:22:10