简体   繁体   English

在 C++ 中使用 STD::List

[英]using STD::List in c++

well I am making a Sudoku puzzle, and I am trying to compare whether a number can go in a row because of the number already existing in a row, and im very new to c++ and well im okay at c# and I have this好吧,我正在制作一个数独谜题,我正在尝试比较一个数字是否可以连续出现,因为该数字已经存在于一行中,而且我对 C++ 非常陌生,而且我在 C# 方面还可以,我有这个

for (int i= 0; i< fifth.size(); i++)
        {
            for (int c= 0; c<9; c++)
            {
                listConnectordown1[c] == fifth[i];


 //then the number can not go in here because it already exists in the row but not the
block
            }
        }

as you can see im getting the error at listConnectordown1[c] == fifth[i];正如你所看到的,我在listConnectordown1[c] == fifth[i]; because of lack of knowledge with c++ could anybody help me so I can compare them correctly?由于缺乏 C++ 知识,有人可以帮助我,以便我可以正确比较它们吗?

the error is错误是

4   IntelliSense: no operator "[]" matches these operands
        operand types are: std::list<int, std::allocator<int>> [ int ]  j:\08227 acw\ACW\Sudoku\Sudoku\main.cpp 152

I have now changed to a vector given advice, now I don't know how to remove a certain value when it needs to be removed for example我现在已更改为给定建议的向量,现在我不知道如何在需要删除某个值时删除它,例如

int possbileChecks[] = {1,2,3,4,5,6,7,8,9};
            std::vector<int> fifth (possbileChecks, possbileChecks + sizeof(possbileChecks) / sizeof(int) );
        for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            PossibleChecks = Block1[i][j];

            if (!PossibleChecks == 0)
            {
            PossibleValuesInsideBlock[count] = PossibleChecks;
            //ValueRemove = count-1;
            possiblValuesCount = PossibleChecks-1;
            fifth.Remove(PossibleChecks);
            possiblValuesCount=0;

            count = count++;
            }   

        }
        }

std::list doesn't provide index access functionality. std::list不提供索引访问功能。 It is implemented as a doubly linked list, so if you want to randomly access elements in the list it's probably not the right choice for you.它被实现为一个双向链表,所以如果你想随机访问列表中的元素,它可能不是你的正确选择。 What you probably want is std::vector :你可能想要的是std::vector

std::vector<int> v(1);
v[0] = 0;

if(v[0] == 0)...
    ... // etc

If you want to stick to the list you should use iterator functions, like begin and advance .如果你想坚持这个列表,你应该使用迭代器函数,比如beginadvance

std::List in c++ is not the same as a list in C#, you aren't able to loop through list like this. c++ 中的 std::List 与 C# 中的列表不同,您无法像这样循环列表。

to iterate through std::list you will have to use an iterator(std::list::Iterator):要遍历 std::list,您必须使用迭代器(std::list::Iterator):

    list<int>::const_iterator it;
    for(it = list1.begin();it != list1.end();it++)
    {
        for(list<int>::const_iterator c = list1.begin();c != list1.end();c++)
        {
            c == it;
        }
    }

or use a range based for loop:或使用基于范围的 for 循环:

    //Range based for loop
    for(int i :list1)
    {
        for(int c : list2)
        {
            i == c;
        }
    }

or just use a std::vector instead of std::list and your code will work或者只是使用 std::vector 而不是 std::list 并且您的代码将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM