std :: ostream {aka std :: basic_ostream< char>}值为'std :: basic_ostream< char>&& [英] std::ostream {aka std::basic_ostream<char>} Ivalue to 'std::basic_ostream<char>&&
问题描述
在此代码中,我尝试将迭代器移动10个元素。
In this code I try to move the iterator by 10 elements.
#include <iostream>
#include <string>
#include <vector>
int main()
{
using namespace std;
vector<int> v(20);
auto mid = v.begin() + 10;
cout<<mid;
}
运行此代码时,错误在标题中提到。
我是一个初学者。我在几乎每个程序我写这个错误。
On running this code, I get the error mentioned in the title. I'm a beginner. I experience this error in almost every program I write. Where am I going wrong?
推荐答案
迭代器指向一个元素,你想做什么是
An iterator "points" to an element, what you want to be doing is:
cout << *mid;
您必须解引用迭代器打印它指向的内容。尝试直接打印它会给你提到的错误。
You have to "dereference" the iterator to print what it points to. Trying to print it directly gives you the error you mentioned.
编辑:这里有一个演示:
here's a little demo:
#include <iostream>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<int> numbers;
numbers.push_back(4);
numbers.push_back(3);
numbers.push_back(2);
auto beg = numbers.begin();
auto mid = numbers.begin() + 1;
std::cout << *beg << std::endl;
std::cout << (beg < mid) << std::endl; // True because beg (index 0) points to an element earlier than mid (index 1)
std::cout << (*beg < *mid) << std::endl; // False because the element pointed-to by beg (4) is bigger than the one pointed-to by mid (3)
return 0;
}
输出
第一行显示4是第一个元素的值!第二行显示1(所有非零值均为真),最后一行显示0(零是唯一表示false的值)。
Output The first line shows 4 which is the value of the first element! The second line shows 1 (all non-zero values mean true) and the last line shows 0 (zero is the only value that means false).
这篇关于std :: ostream {aka std :: basic_ostream< char>}值为'std :: basic_ostream< char>&&的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!