C++ 无法遍历向量

C++ 无法遍历向量,c++,vector,stl,C++,Vector,Stl,嘿,我正在尝试将给定的输入排序到一个波形数组中 给定一个整数数组,将数组排序为波浪状数组并返回, 换言之,将元素排列成一个序列,使a1>=a2=a4 a[it]) { 内部温度=0; 温度=A[it+1]; A[it+1]=A[it]; A[它]=温度; } } } } } 错误:- solution.cpp: In member function 'std::vector<int> Solution::wave(std::vector<int>&)': sol

嘿,我正在尝试将给定的输入排序到一个波形数组中

给定一个整数数组,将数组排序为波浪状数组并返回, 换言之,将元素排列成一个序列,使a1>=a2=a4 a[it]) { 内部温度=0; 温度=A[it+1]; A[it+1]=A[it]; A[它]=温度; } } } } } 错误:-

solution.cpp: In member function 'std::vector<int> Solution::wave(std::vector<int>&)':
solution.cpp:8:13: error: no match for 'operator[]' (operand types are 'std::vector<int>' and '__gnu_cxx::__normal_iterator<int*, std::vector<int> >')
         if(A[it+1]>A[it])
             ^
solution.cpp:8:13: note: candidates are:
In file included from /usr/include/c++/4.8/vector:64:0,
                 from solution.h:13,
                 from solution.cpp:-3:
/usr/include/c++/4.8/bits/stl_vector.h:770:7: note: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::reference = int&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]
       operator[](size_type __n)
       ^
/usr/include/c++/4.8/bits/stl_vector.h:770:7: note:   no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' to 'std::vector<int>::size_type {aka long unsigned int}'
/usr/include/c++/4.8/bits/stl_vector.h:785:7: note: std::vector<_Tp, _Alloc>::const_reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) const [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::const_reference = const int&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]
       operator[](size_type __n) const
   ^
solution.cpp:在成员函数“std::vector solution::wave(std::vector&)”中:
解决方案.cpp:8:13:错误:与“运算符[]”不匹配(操作数类型为“std::vector”和“\uu gnu\u cxx::\uu normal\u迭代器”)
如果(A[it+1]>A[it])
^
解决方案。cpp:8:13:注:候选人为:
在/usr/include/c++/4.8/vector:64:0中包含的文件中,
从溶液h:13,
来自解决方案。cpp:-3:
/usr/include/c++/4.8/bits/stl_vector.h:770:7:注:std::vector::reference std::vector::operator[](std::vector::size_type)[带Tp=int;Alloc=std::分配器;std::vector::reference=int&;std::vector::size_type=long unsigned int]
运算符[](大小类型)
^
/usr/include/c++/4.8/bits/stl_vector.h:770:7:注意:参数1从“u gnu_cxx::u normal_迭代器”到“std::vector::size_type{aka long unsigned int}”的转换未知
/usr/include/c++/4.8/bits/stl_vector.h:785:7:注:std::vector::const_reference std::vector::operator[](std::vector::size_type)const[with(with)Tp=int;(u Alloc=std::分配器;std::vector::const_reference=const int&;std::vector::size_type=long unsigned int]
运算符[](大小类型)常量
^
Source:-

A.begin()返回指向向量开头的迭代器,因此可以使用*A.begin()访问元素


因此,在这种情况下,[it+1]没有意义。

当访问向量元素时,您可以使用迭代器:

for( auto it = v.begin(); it != v.end(); ++it )
    std::cout << *it;
for(自动it=v.begin();it!=v.end();++it)

STD::CUT< P> C++中的迭代器是容器中某个位置的引用的包装器。code>it
不是整数,但在本例中,
vector
it
指整数

因此,
it
不能用作下标索引,但
*it
可以

it+1
不会解析为
it
处的数字加1,而是解析为
it
之后的向量元素的迭代器。你可能是想写信

if(A[*(it+1)]>A[*it])
把它分解

  • it+1
    :it之后的参考元素。我们把这个元素称为1
  • *(it+1)
    :在
    it
    之后的元素处获取值。从头到尾 在上面,这与
    A[1]
    相同,假设
    A[1]
    她42岁
  • A[*(it+1)]
    :is
    A[A[1]
    A[42]
  • 这将编译,但它不会执行您似乎正在尝试执行的操作

    相反,您可能希望直接比较整数

    if(*(it+1)>*it)
    
    对于代码中的
    A[it]
    的所有后续变体也类似

    这将允许您调试算法中的其他问题,这是我不打算解决的问题,因为这看起来像是某种分配或挑战

    除此之外:

    for (auto it = A.begin(); it != A.end(); it++)
    
    允许
    向量的开头一直到结尾,并且需要额外的保护,以便
    *(it+1)
    在到达
    向量
    的结尾时无法到达
    向量
    的结尾。你可以

    for (auto it = A.begin(); it != A.end() - 1; it++)
    

    但是,如果你四处看看,你可能会找到一个更好的选择。

    你的循环有一些问题

    它是一个迭代器。要得到it的值是*it,it+1的内容是*(it+1)

    您的循环应该只转到size-1,否则将把最后一个值与无效值进行比较

    std::swap()将比手工编码的交换更有效,因为它将使用移动而不是复制

    您可能打算将循环的值增加2,以便在for循环中它+=2

    您应该检查奇数长度并采取适当的措施

    迈克

    if(*(it+1)>*it)
    
    for (auto it = A.begin(); it != A.end(); it++)
    
    for (auto it = A.begin(); it != A.end() - 1; it++)