C++ 如何跳转到for循环的末尾,但不离开它,使用goto
我有一个函数,可以找到向量中的所有多个元素。如果我发送C++ 如何跳转到for循环的末尾,但不离开它,使用goto,c++,stdvector,goto,C++,Stdvector,Goto,我有一个函数,可以找到向量中的所有多个元素。如果我发送{1,2,3,4,5,1,2,3,3,7}它返回{1,2,3}。 我的输入向量大约有100到10000个元素,但我希望只有很少不同的(!)副本;大约1-5% 因此,如果我已经将一个元素标识为多次出现,那么我将对照我的复制向量进行检查。如果是这样,功能应继续到下一个元素(如果有)。为此,我使用了一个goto 但是我需要在转到标签之后有一个命令。否则编译器会抱怨。有没有办法避免这种情况并保持这种状态?我知道我可以使用其他方法,例如相应地设置boo
{1,2,3,4,5,1,2,3,3,7}
它返回{1,2,3}
。
我的输入向量大约有100到10000个元素,但我希望只有很少不同的(!)副本;大约1-5%
因此,如果我已经将一个元素标识为多次出现,那么我将对照我的复制向量进行检查。如果是这样,功能应继续到下一个元素(如果有)。为此,我使用了一个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;
}
vector FindPublicates(vector const&v){
//例如{1,2,3,4,5,1,2,3,7}->{1,2,3}
双硅酸盐载体;
for(自动开始);it!=v.end()-1;
++它){//遍历除最后一个元素之外的每个元素
对于(自动常数和i:
dublicates){//检查这是否已经是已知的dublicate
如果(i==*it)
转到下一个元素;//如果是,转到v中的下一个元素
}
对于(自动it2(it+1);it2!=v.end();
++it2){//否则将其与v中的“未检查”元素进行比较
如果(*it==*it2){//如果找到了副本,则保留它
重复。向后放置(*it);
break;//检查v中的下一个元素;也可以使用goto
//下一步
}
}
nextElement:
cout您应该能够使用分号作为no-op
nextElement:
;
}
但是,我不确定查找重复项的方法是否有效。最好对数组进行排序,然后迭代一次。对向量进行排序会将所有重复项分组在一起。然后只需检查当前元素是否与前一个元素相同。您应该能够使用分号作为禁止行动
nextElement:
;
}
但是,我不确定您查找重复项的方法是否有效。您最好先对数组进行排序,然后对其进行一次迭代。对向量进行排序将所有重复项分组在一起。然后您只需检查当前元素是否与前一个元素相同。如果删除转到
将不会成功请尝试使用布尔辅助变量:
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"
}
for(auto-it(v.begin());it!=v.end()-1;++it){//遍历除最后一个之外的每个元素
bool-found=false;
对于(auto const&i:dublicates){//检查这是否已经是已知的dublicate
如果(i==*it)
{
发现=真;
打破
}
}
如果(!找到)
{
对于(autoit2(it+1);it2!=v.end();++it2){//否则将其与v中的“notchecked”元素进行比较
如果(*it==*it2){//如果找到了副本,则保留它
重复。向后放置(*it);
break;//检查v中的下一个元素;也可以使用goto nextElement
}
}
}
cout如果删除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"
}
for(auto-it(v.begin());it!=v.end()-1;++it){//遍历除最后一个之外的每个元素
bool-found=false;
对于(auto const&i:dublicates){//检查这是否已经是已知的dublicate
如果(i==*it)
{
发现=真;
打破
}
}
如果(!找到)
{
对于(autoit2(it+1);it2!=v.end();++it2){//否则将其与v中的“notchecked”元素进行比较
如果(*it==*it2){//如果找到了副本,则保留它
重复。向后放置(*it);
break;//检查v中的下一个元素;也可以使用goto nextElement
}
}
}
cout我建议重新构造代码,不要使用goto。goto可能很有用,但它们不受欢迎,通常可以用更具可读性的结构替换
考虑:
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;
}
bool-ispublicate(整数候选、向量常量和副本)
{
适用于(自动施工和验收:副本)
如果(i==候选者)返回true;
返回false;
}
矢量FindPublicates(矢量常量和v){
//e、 g.{1,2,3,4,5,1,2,3,7}->{1,2,3}
双硅酸盐载体;
对于(auto-it(v.begin());it!=v.end()-1;++it){//遍历除最后一个之外的每个元素
如果(IsPublicate(*其副本))
继续;
对于(autoit2(it+1);it2!=v.end();++it2){//否则将其与v中的“notchecked”元素进行比较
如果(*it==*it2){//如果找到了副本,则保留它
重复。向后放置(*it);
break;//检查v中的下一个元素;也可以使用goto nextElement
}
}
}
返回副本;
}
我建议重新构造代码,不要使用goto。goto可能很有用,但它们不受欢迎,通常可以用更具可读性的结构替换
考虑:
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;
}
bool-ispublicate(整数候选、向量常量和副本)
{
适用于(自动施工和验收:副本)
如果(i==候选者)返回true;
返回false;
}
矢量FindPublicates(矢量常量和v){
//e、 g.{1,2,3,4,5,1,2,3,7}->{1,2,3}
双硅酸盐载体;
对于(auto-it(v.begin());it!=v.end()-1;++it){//遍历除最后一个之外的每个元素
如果(IsPublicate(*其副本))
继续;
对于(autoit2(it+1);it2!=v.end();++it2){//否则将其与v中的“notchecked”元素进行比较
如果(*it==*it2){//如果找到了副本,则保留它
重复。向后放置(*it);
break;//检查v中的下一个元素;也可以使用goto nextElement
}
}
}
返回副本;
}
该命令可以是一个分号。这是一个null语句,但它应该满足编译器的要求