如何用goto跳到for循环的末尾,但不离开它
How to jump to the end of a for loop, but not leaving it, with goto
我有一个函数,可以查找向量中的所有多个元素。如果我发送{1,2,3,4,5,1,2,3,3,7}
,它将返回{1,2,3}
。我的输入向量大约有100到10000个元素,但我希望只有很少不同的(!)重复;约1-5%。
因此,如果我已经识别出一个元素发生了多次,我会对照我的重复向量进行检查。如果是,则函数应转到下一个元素(如果有)。为此,我使用goto
。
但我需要在goto label
之后有一个命令。否则编译器会抱怨。有什么方法可以避免这种情况并保持goto?我知道我可以使用其他方法,例如相应地设置bool和使用if()。然而,我认为goto方法是直接的。
vector<int> findDublicates(vector<int> const& v) {
// e.g. {1,2,3,4,5,1,2,3,7} -> {1,2,3}
vector<int> dublicates;
for (auto it(v.begin()); it != v.end() - 1;
++it) { // go through each element except the last
for (auto const& i :
dublicates) { // check if this is already a known dublicate
if (i == *it)
goto nextElement; // if so, goto the next element in v
}
for (auto it2(it + 1); it2 != v.end();
++it2) { // else compare it with the "not checked" elements in v
if (*it == *it2) { // if a dublicate is found, keep it
dublicates.emplace_back(*it);
break; // check the next element in v; could also use goto
// nextElement
}
}
nextElement:
cout << " "; // if I remove cout it won't compile: "expected
// primary-expression before '}' token"
}
return dublicates;
}
您应该能够使用分号作为no-op。
nextElement:
;
}
然而,我不确定你的方法是否找到重复是有效的。您最好先对数组进行排序,然后迭代一次。对矢量进行排序将把所有重复项组合在一起。然后,您只需检查当前元素是否与前一个元素相同。
如果删除goto
不会杀死您,请尝试使用布尔辅助变量:
for(auto it(v.begin()); it!=v.end()-1; ++it) { //go through each element except the last
bool found = false;
for(auto const &i : dublicates) { //check if this is already a known dublicate
if(i==*it)
{
found = true;
break;
}
}
if (!found)
{
for(auto it2(it+1); it2!=v.end(); ++it2) { //else compare it with the "not checked" elements in v
if(*it==*it2) { //if a dublicate is found, keep it
dublicates.emplace_back(*it);
break; //check the next element in v; could also use goto nextElement
}
}
}
cout<<" "; //if I remove cout it won't compile: "expected primary-expression before '}' token"
}
在编程中使用goto
被认为是不好的形式。在网上搜索"意大利面条代码goto"。
我建议重新构造代码,使其不使用goto。Gotos可能很有用,但它们不受欢迎,通常可以用更可读的结构来代替。
考虑:
bool isDublicate( int candidate, vector<int> const & dublicates )
{
for ( auto const &i: dublicates )
if ( i == candidate ) return true;
return false;
}
vector<int> findDublicates(vector<int> const &v) {
//e.g. {1,2,3,4,5,1,2,3,7} -> {1,2,3}
vector<int> dublicates;
for(auto it(v.begin()); it!=v.end()-1; ++it) { //go through each element except the last
if ( isDublicate( *it, dublicates ) )
continue;
for(auto it2(it+1); it2!=v.end(); ++it2) { //else compare it with the "not checked" elements in v
if(*it==*it2) { //if a dublicate is found, keep it
dublicates.emplace_back(*it);
break; //check the next element in v; could also use goto nextElement
}
}
}
return dublicates;
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 我的神经网络不起作用 [XOR 问题]
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- QSqlquery prepare()和bindvalue()不工作
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 检查输入是否不是整数或数字
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 使用不带参数的函数访问结构元素
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 我的boost managed_shared_memory在离开作用域时似乎不会从内存空间中取消映射
- c++ 指针在离开作用域后不会停留
- C++ std::stack 内容在离开函数时不会自由
- 如何用goto跳到for循环的末尾,但不离开它
- 离开 c++ 应用程序而不运行全局静态对象的析构函数
- Makefile 离开 .o 并且不生成可执行文件
- 当我离开该方法时,来自静态向量的数据保持不变