C++;循环条件不存在';不要结束循环 我试图将ARTMEIS组件实体系统移植到C++中,学习一些东西。但是我遇到了一个奇怪的问题 void addAll(ImmutableBagInterface<E>& items) { for(auto i = 0u; i < items.size(); ++i) { add(items.get(i)); } }

C++;循环条件不存在';不要结束循环 我试图将ARTMEIS组件实体系统移植到C++中,学习一些东西。但是我遇到了一个奇怪的问题 void addAll(ImmutableBagInterface<E>& items) { for(auto i = 0u; i < items.size(); ++i) { add(items.get(i)); } },c++,for-loop,infinite-loop,C++,For Loop,Infinite Loop,以下是出现问题的测试: #include <iostream> #include <string> #include "ImmutableBag.h" int main() { using namespace artemis; Bag<std::string> bag; Bag<std::string> bag2; for(auto i = 0u; i < 20; i++) { ba

以下是出现问题的测试:

#include <iostream>
#include <string>
#include "ImmutableBag.h"

int main()
{
    using namespace artemis;
    Bag<std::string> bag;
    Bag<std::string> bag2;

    for(auto i = 0u; i < 20; i++)
    {
        bag2.add("Test");
    }

    bag2.add("Hello");
    bag2.add("World");

    bag.add("Hello");
    std::cout << bag.get(0) << std::endl;
    bag.add("World");
    std::cout << bag.get(1) << std::endl;

    for(auto i = 0u; i < bag2.size(); i++)
    {
        std::cout << bag2.get(i) << std::endl;
    }
    std::cout << "==========================" << std::endl;

    bag2.removeAll(bag); //Executes normally (Removes all items, which are identical to any of the items in bag, from bag2)

    for(auto i = 0u; i < bag2.size(); i++)
    {
        std::cout << bag2.get(i) << std::endl;
    }

    std::cout << "==========================" << std::endl;

    bag.addAll(bag2); //Infinite loop... NOPE I was an idiot and wrote bag.addAll(bag) instead of bag.addAll(bag2)as Mike Seymour and Thynk Apps pointed out 

    for(auto i = 0u; i < bag.size(); ++i)
    {
        std::cout << bag.get(i) << std::endl;
    }


    return 0;
}
#包括
#包括
#包括“ImmutableBag.h”
int main()
{
使用名称空间artemis;
袋子;
袋袋2;
用于(自动i=0u;i<20;i++)
{
bag2.添加(“测试”);
}
bag2.添加(“你好”);
bag2.添加(“世界”);
bag.add(“你好”);

std::cout我不知道你的
add()
函数在做什么,但是你知道如果你在这个循环中添加到你正在循环的容器中,你会把它的大小增加1,这样for循环就可以(逻辑上)了永远不要终止?不过,在终止之前,您很可能会耗尽内存。

除非
items.size()
开始时为零…=)但可能是问题,是的,只有当传入的项目列表等于您要添加到的容器时,这才是问题。我不知道为什么您要将所有项目添加到同一容器中,但您可以通过设置
initialSize=items.size()来解决此问题
然后使用
i
作为循环condition@Michael但是,我知道循环条件不是当前容器的大小,而是传递给方法的容器的大小。因此,由于没有对该容器进行任何更改,我应该在将所有项添加到当前容器后顺利退出。
add()
方法只是围绕底层向量的
push_back()
,所以它只是将项目添加到包的末尾;@mark&Thynk,在所有方面我都同意:)@MrPlow,除非您在get函数中更改I(并通过引用获取它),我看不出它是如何永远循环的。你能把它做成一个a,这样我们就可以看到
addAll
是如何被调用的。你不是在试图给自己添加一个包,是吗?如果你尝试这样做,
addAll
的实现将永远循环。@ShafikYaghmour我添加了一个测试用例。从你刚刚发布的代码中。你正在做你不能做的事情。您正在将
bag
添加到
bag
中,这将使循环中的大小无限增加1。如果这是您想要做的,您可以通过在循环之前获取初始大小或将其作为第二个参数传入来将其设置为循环条件。@ThynkApps是的……我是个白痴……我没有注意到我忘记添加“2”
#include <iostream>
#include <string>
#include "ImmutableBag.h"

int main()
{
    using namespace artemis;
    Bag<std::string> bag;
    Bag<std::string> bag2;

    for(auto i = 0u; i < 20; i++)
    {
        bag2.add("Test");
    }

    bag2.add("Hello");
    bag2.add("World");

    bag.add("Hello");
    std::cout << bag.get(0) << std::endl;
    bag.add("World");
    std::cout << bag.get(1) << std::endl;

    for(auto i = 0u; i < bag2.size(); i++)
    {
        std::cout << bag2.get(i) << std::endl;
    }
    std::cout << "==========================" << std::endl;

    bag2.removeAll(bag); //Executes normally (Removes all items, which are identical to any of the items in bag, from bag2)

    for(auto i = 0u; i < bag2.size(); i++)
    {
        std::cout << bag2.get(i) << std::endl;
    }

    std::cout << "==========================" << std::endl;

    bag.addAll(bag2); //Infinite loop... NOPE I was an idiot and wrote bag.addAll(bag) instead of bag.addAll(bag2)as Mike Seymour and Thynk Apps pointed out 

    for(auto i = 0u; i < bag.size(); ++i)
    {
        std::cout << bag.get(i) << std::endl;
    }


    return 0;
}