C++ 在矢量中删除和替换
C++ remove & replace in a vector
我有一个向量的向量:
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
相关文章:
- 删除/替换C++字符串中的多字符 (ÿû)
- Denstructor删除已被替换的成员
- 将一个字符替换为字符串中更多数量的字符,而不删除C++中的其他字母
- 删除两个相邻的元素并用矢量中的单个元素替换它们
- Visual C 以前的文本在终端中删除并替换为新文本
- C++ 字符串,用于删除字符'b'并将字符'a'替换为两个'd'的构建函数
- 如何将'malloc'和'free'替换为'new'和"删除"?
- 输出流中的第一个字符将替换为该流中被删除的最后一个字符.为什么会这样
- 当我替换对象时,我需要删除我在类成员中设置的对象吗
- 替换C++中的删除,信息缺失
- 如何删除shared_ptr内容并将其替换为新对象
- 如何替换删除和删除[]
- C++可变模板:删除/替换索引处的类型
- Clang链接时间优化与替换的操作员新的原因不匹配的自由()/删除在valgrind
- 如何使用c++更改,替换或删除文本文件中的一行
- 匹配字符串长度;查找和替换;在c++中删除HTML标签
- 如何正确替换全局新建和删除运算符
- 在llvm中用Undef值替换所有要删除的指令
- 删除和替换catarray中间的对象指针
- "LL"可以从硬编码的整数常量中删除,并替换为"static_cast<uint64_t>(...)"吗?