无法移动设置迭代器

时间:2015-09-06 16:46:56

标签: c++ set

我做错了什么:

//Practicing classes.

#include  <iostream>
#include <string>

using namespace std;


//Class decleration
class person
{
    //Access Specifier
    public: 
        string name;    //Member Vairable Decleration
        int number;     //Member Function Decleration
};

//Main function.
int main()
{
    //Object creation from class.
    person obj;

    //Get input Values for object Variables
    cout<<"Enter the Name: \n";
    cin>>obj.name;
    cin.getline(obj.name);

    cout<<"Enter the number:\n";
    cin>>obj.number;

    //Show the output
    cout<< obj.name <<": " << obj.number << endl;

    return 0;
}

任何想法为什么?

1 个答案:

答案 0 :(得分:1)

推进迭代器的正确方法是使用std::advancestd::next

beg = std::next(beg, 3);
std::advance(beg, 3);

使用+=递增迭代器的方法仅适用于由于指针算法而导致的数组(或带有random access iterators的容器)。