gpt4 book ai didi

C++在 vector 中删除和替换

转载 作者:行者123 更新时间:2023-11-28 07:43:27 24 4
gpt4 key购买 nike

我有一个这样的 vector vector :

一个: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

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

谢谢

最佳答案

你在那里犯了几个错误。如果您只想将前导 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;
});

// ...
}

关于C++在 vector 中删除和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15358436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com