[英]Why does " error: no match for 'operator<<' " occur when searching for the iterator?
我正在使用find();
方法,但由於某種原因,它與cout<<
沖突,我不明白為什么。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, busquedas, num,
vas=0, pet=0;
vector <int> arreglo;
cin>>n;
for(int i = 0; i < n; i++){
cin>> num;
arreglo.push_back(num);
}
cin>>busquedas;
for(int i = 0; i < busquedas; i++){
cin>>num;
std::vector<int>::iterator it = std::find(arreglo.begin(), arreglo.end(), num);
cout<<it; //Here the error occurs
}
return 0; }
我也嘗試將iterator
更改為auto
類型,它顯示相同的錯誤。
此錯誤意味着帶有std::cout
的迭代器類型不支持<<
運算符。 如果您想查看迭代器與向量開頭的距離,請嘗試執行此操作。
std::cout << std::distance(arreglo.begin(), it) << std::endl;
或者,如果您想要迭代器指向的數組中的值(感謝@Slava 避免段錯誤的想法)。
if (it != arreglo.end())
std::cout << *it << std::endl;
嘿,您不能打印迭代器 << 運算符並沒有為此而重載。
您可以在其中打印值it - vec.begin()
它將在 Vector 中打印元素的索引
您還應該檢查it.= vec.end()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.