由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问一个C++ set和unordered_set iterator的问题
相关主题
c++ iterator 弱问C++ 模板编译错误?
这段C++代码有啥问题C++ 用户定义exception的标准用法是什么?
C++一个string的小问题请教一个boost::bind的问题
return Triangular_iterator( _beg_pos );意思const_reverse_iterator和reverse_iterator有什么区别? (转载)
基本功不扎实,问个问题形参可以直接使用私有数据成员?
C++ class cross reference problem请教一个C++问题
一个C++的概念问题再问两个C++问题
这段 C++ 怎么改才能编译?VC++ 6.0 弱问,多谢解答
相关话题的讨论汇总
话题: string话题: set话题: iterator话题: foo话题: void
进入Programming版参与讨论
1 (共1页)
e*******s
发帖数: 1979
1
以下这段代码 把set的iterator直接传递到parameter为reference的函数里
报错
test.cpp: In function 'int main()':
test.cpp:110: error: invalid initialization of reference of type 'std::
string&' from expression of type 'const std::basic_string traits, std::allocator >'
test.cpp:67: error: in passing argument 1 of 'void foo(std::string&)'
make: *** [a] Error 1
如果修改代码
1. void foo(string &s) --> void foo(string s)
2. string s = *it; foo(s);
3. void foo(string &s) --> void foo(const string &s)
4. unordered_set vs --> vector vs;
都不会报错.
原因是什么呢, 1. 2.可以理解为dereference不能传入reference parameter中去,因
为可能会引用局部变量
3. 可以理解为不能修改set::iterator指向都局部变量
4. 就很奇葩了, vector::iterator 和 set::iterator的区别不应该在dereference上把
在标准上好像就是random access iterator 和 bidirectional iterator的区别
编译器是 gcc 4.4.2
void foo(string &s)
{
return;
}
unordered_set vs;
for(auto it = vs.begin(); it != vs.end(); ++it){
foo(*it);
}
S**I
发帖数: 15689
2
The error message is quite clear: elements in set are const variables,
cannot be dereferenced to non-const.

【在 e*******s 的大作中提到】
: 以下这段代码 把set的iterator直接传递到parameter为reference的函数里
: 报错
: test.cpp: In function 'int main()':
: test.cpp:110: error: invalid initialization of reference of type 'std::
: string&' from expression of type 'const std::basic_string: traits, std::allocator >'
: test.cpp:67: error: in passing argument 1 of 'void foo(std::string&)'
: make: *** [a] Error 1
: 如果修改代码
: 1. void foo(string &s) --> void foo(string s)

l*********8
发帖数: 4642
3
yes, unordered_set 或者set的iterator指向的内容是const的。
否则, 如果你可以修改*it, 那么*it在hash map or bst里面的位置就要改变了,那么
这个iterator就要变成invalid了。 乱套了。

【在 S**I 的大作中提到】
: The error message is quite clear: elements in set are const variables,
: cannot be dereferenced to non-const.

e*******s
发帖数: 1979
4
good thanks!

【在 S**I 的大作中提到】
: The error message is quite clear: elements in set are const variables,
: cannot be dereferenced to non-const.

e*******s
发帖数: 1979
5
thanks!

【在 l*********8 的大作中提到】
: yes, unordered_set 或者set的iterator指向的内容是const的。
: 否则, 如果你可以修改*it, 那么*it在hash map or bst里面的位置就要改变了,那么
: 这个iterator就要变成invalid了。 乱套了。

a*********a
发帖数: 3656
6
sets are ordered. allowing modification of set elements via iterators would
violate the order, unless the set is re-sorted every time. the decision was
made by the committee to simply disallow nonconst reference to set elements.
there was an article in C++ user's journal about this long long ago.
set s;
s.insert(1);
s.insert(2);
s.insert(3);
set::iterator i=s.begin();
i++;
*i = 4; // s is no longer ordered!
for unordered_set, I guess allowing nonconst ref to elements would
invalidate the hash key.
e*******s
发帖数: 1979
7
thank you!

would
was
elements.

【在 a*********a 的大作中提到】
: sets are ordered. allowing modification of set elements via iterators would
: violate the order, unless the set is re-sorted every time. the decision was
: made by the committee to simply disallow nonconst reference to set elements.
: there was an article in C++ user's journal about this long long ago.
: set s;
: s.insert(1);
: s.insert(2);
: s.insert(3);
: set::iterator i=s.begin();
: i++;

1 (共1页)
进入Programming版参与讨论
相关主题
VC++ 6.0 弱问,多谢解答基本功不扎实,问个问题
一个C++ operator new的重载问题C++ class cross reference problem
C++如何快速输入iterator类型名一个C++的概念问题
how to read a sentence into a vector of string?这段 C++ 怎么改才能编译?
c++ iterator 弱问C++ 模板编译错误?
这段C++代码有啥问题C++ 用户定义exception的标准用法是什么?
C++一个string的小问题请教一个boost::bind的问题
return Triangular_iterator( _beg_pos );意思const_reverse_iterator和reverse_iterator有什么区别? (转载)
相关话题的讨论汇总
话题: string话题: set话题: iterator话题: foo话题: void