矢量上的错误
Error On vector
本文关键字:错误 更新时间:2023-10-16
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)
std::lower_bound
返回一个std::vector<int>::iterator
,你不能使用cout
打印它。
可能你的意思是:
cout << "Yes " << *itr1 << endl;
cout << "No " << *itr1 << endl;
如果你想打印位置,你应该做这样的事情。
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';
我希望它有所帮助。
迭代器不能传递给std::cout
。
为了得到正确的位置,我们需要从itr1
中减去v.begin()
,即:
cout << distance(v.begin(), itr1) << endl;
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'