【问题标题】:Error On vector矢量错误
【发布时间】:2016-11-07 17:34:24
【问题描述】:
vector<int> :: iterator itr1;
cin >> query;
for(i = 0; i < query ; i++)
{
    cin >> checknum;
    if (binary_search (v.begin(), v.end(), checknum))
    {
        itr1 = lower_bound(v.begin(), v.end(), checknum);
        cout << "Yes " << itr1 << endl;
    }
    else
    {
        itr1 = lower_bound(v.begin(), v.end(), checknum);
        cout << "No " << itr1 << endl;
    }
}

我在编译过程中遇到错误:编译消息

solution.cc: In function 'int main()':
solution.cc:28:18: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
             cout << "Yes " << itr1 << endl;
                  ^
In file included from /usr/include/c++/4.9/iostream:39:0,
                 from solution.cc:4:
/usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]'
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^
solution.cc:33:18: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
             cout << "No " << itr1 << endl;
                  ^
In file included from /usr/include/c++/4.9/iostream:39:0,
                 from solution.cc:4:
/usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]'
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)

【问题讨论】:

  • 您可以将其缩小到 2 行代码。您发布的大部分内容都是无关紧要的。
  • 那么问题是什么?除此之外,您可能还想在 cout 语句中使用 *itr1。

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


【解决方案1】:

std::lower_bound 返回一个std::vector&lt;int&gt;::iterator,您不能使用cout 打印它。

你的意思可能是:

cout << "Yes " << *itr1 << endl;
cout << "No " << *itr1 << endl;

【讨论】:

  • lower_bound 返回什么都没关系。不相关的代码也给你了!
【解决方案2】:

无法将迭代器传递给std::cout。

为了得到正确的位置,我们需要从itr1中减去v.begin(), 即:

cout << distance(v.begin(), itr1) << endl;

【讨论】:

    【解决方案3】:

    如果你想打印位置,你应该这样做。

    std::vector<int>::iterator low,up;
    low=std::lower_bound (v.begin(), v.end(), 20); 
    up= std::upper_bound (v.begin(), v.end(), 20); 
    
    std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
    std::cout << "upper_bound at position " << (up - v.begin()) << '\n';
    

    希望对你有帮助。

    【讨论】:

    • 为什么投反对票?好的。写const auto low = std::lower_bound(v.begin(), v.end(), 20); 会更好,但基本答案是正确的。
    • 我有点担心,有什么办法吗
    • 谢谢链接,我正在搜索但找不到它
    • low - v.begin() 和 up - v.begin() 生成迭代器,不能传递给 std:cout。如果要输出low和up代表的向量索引,使用std::distance(),例如:std::cout &lt;&lt; "lower_bound at position " &lt;&lt; std::distance(v.begin(), low) &lt;&lt; std::endl;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-19
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多