假设我们有以下内容:
// Example program
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector <int> v {1,2,3};
vector <int>::iterator v_i1 = v.begin();
vector <int>::iterator &v_i2 = v.begin(); // error out - initial value of reference to non-const must be an lvalue
}
理解:
感谢您帮助我更好地理解。
非常量引用无法通过
std::vector::begin()
绑定到右值 returnrd。使引用常量。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector <int> v {1,2,3};
vector <int>::iterator v_i1 = v.begin();
const vector <int>::iterator &v_i2 = v.begin();
}