不匹配operator [](std :: vector)

时间:2018-02-07 17:22:11

标签: c++ c++11 vector stl

所以,我使用邻接列表制作了一个图表,我试图通过使用递归来搜索它。收到一条尴尬的错误消息,表示'与运营商[]'不匹配。这是代码:

#include <iostream>
#include <vector>
using namespace std;


void search(vector <int> *v,int node)
{
    if (node==4)
    {
        cout<<"found 4";
        return;
    }

    vector <int> :: iterator it;
    if (!v[node].empty())
    {
        for (it=v[node].begin() ; it!=v[node].end() ; it++)
        {
            search(v,v[node][it]);
        }
    }
}

int main()
{
    vector <int> v[5];
    v[1].push_back(2);
    v[1].push_back(3);
    v[2].push_back(4);
    v[2].push_back(5);
    search (v,1);
}

3 个答案:

答案 0 :(得分:1)

变量it是一个迭代器。

vector <int> :: iterator it;

这不能用于索引到这样的数组:

search(v,v[node][it]); // Expecting an index not an iterator.

我认为你想要的是取消引用迭代器。

search(v, *it);

答案 1 :(得分:1)

您必须检查节点是否不在界限范围内。

试试这个:

    >>> import tensorflow as tf
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\pywrap_ten
sorflow_internal.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in impor
t_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 648, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 560, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, in create_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
ImportError: DLL load failed with error code -1073741795

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\pywrap_ten
sorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\pywrap_ten
sorflow_internal.py", line 21, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\pywrap_ten
sorflow_internal.py", line 20, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in impor
t_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\__init__.py", lin
e 24, in <module>
    from tensorflow.python import *
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\__init__.p
y", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\pywrap_ten
sorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\pywrap_ten
sorflow_internal.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in impor
t_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 648, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 560, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 922, in create_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
ImportError: DLL load failed with error code -1073741795

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\pywrap_ten
sorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\pywrap_ten
sorflow_internal.py", line 21, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\pywrap_ten
sorflow_internal.py", line 20, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "C:\Program Files\Python36\lib\importlib\__init__.py", line 126, in impor
t_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_probl
ems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

答案 2 :(得分:0)

在您的代码中,v是向量的指针(表示数组)。要访问下标运算符,必​​须首先使用v[first_index]访问C样式数组。然后,您必须索引从第一个索引获得的任何返回对象。但是,你甚至不应该使用指针作为向量。如果要修改容器,请通过引用传递它。如果你想存储一个向量数组,那么我建议你只是嵌套它们(虽然这可能对内存有害,所以你可以创建一维向量并计算索引)。