随笔-34  评论-108  文章-0  trackbacks-0

    今天看vector的front这个函数msdn中的例子,给出的例程是这样的:
// vector_front.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;  
   vector <int> v1;
  
   v1.push_back( 10 );
   v1.push_back( 11 );

   int& i = v1.front( );
   const int& ii = v1.front( );

   cout << "The first integer of v1 is "<< i << endl;
   i++;
   cout << "The second integer of v1 is "<< ii << endl;
}
vector::back这个函数给出的例程如下:
// vector_back.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main( )
{
   using namespace std;  
   vector <int> v1;
  
   v1.push_back( 10 );
   v1.push_back( 11 );

   int& i = v1.back( );
   const int& ii = v1.front( );

   cout << "The last integer of v1 is " << i << endl;
   i--;
   cout << "The next-to-last integer of v1 is "<< ii << endl;
}

    其中front函数例程里面,两个输出the first integer和the second integer意思是i是v1中的第一个元素的引用,而ii是v1中第二个元素的引用?back函数例程里面,the last integer和the next-to-last integer两句也是同样的道理。可是front应该都是v1中第一个元素的引用,只是i++;这句之后,ii引用的元素增加了1,恰好和v1中的第二个元素的值相同而已。
    不知道是我的理解有问题,还是真的有问题,各位请指教。

posted on 2007-06-04 09:50 探丫头 阅读(1208) 评论(17)  编辑 收藏 引用 所属分类: 编程语言——C++

评论:
# re: msdn也有错? 2007-06-04 11:00 | su
ii == i
你理解的没有错

msdn上的程序不知道要说明什么  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:03 | Mickey Mouse
谢谢,是啊,我也搞不明白,这个程序是在搞什么  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:09 | foobar
看stl的话,就是到cppreference也比到msdn好吧  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:09 | Mickey Mouse
其实这样的话,我觉得vector就不是那么安全了
如果我在vector中存放的是const int,我通过front获得了一个int&的话,就可以通过这个获得的引用修改vector第一个元素的值,所以在vector存放const变量,是不是就变得没什么意义了呢?  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:10 | Mickey Mouse
@foobar
看stl的话,就是到cppreference也比到msdn好吧

什么意思啊?  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:15 | foobar
@Mickey Mouse
我是感觉msdn在stl方面不如其他文档好  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:18 | Mickey Mouse
@foobar
有可能吧,我很少看其他的文档,对msdn还是比较信任的  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:22 | foobar
@Mickey Mouse
vector 中存放const没有意义?
vector中没有const变量吧  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:32 | Mickey Mouse
vector<const int> intVec;
intVec.push_back(10);
intVec.push_back(20);
intVec.push_back(30);

像上面这样的一系列操作,从理论上分析,这个vector中的三个元素不是const int?  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:52 | foobar
@Mickey Mouse
应该不是const int吧
10 20 30 都是临时变量

  回复  更多评论
  
# re: msdn也有错? 2007-06-04 11:53 | Mickey Mouse
头疼,这些东西内部如何实现的不去追求了,太麻烦了  回复  更多评论
  
# re: msdn也有错? 2007-06-04 13:51 | picasa
的确头痛  回复  更多评论
  
# re: msdn也有错? 2007-06-05 22:05 | To Be C++
# re: msdn也有错? 2007-06-06 08:30 | Mickey Mouse
是啊,看来不能尽信msdn呢,以前没有辨别能力的时候可是完全相信msdn的
  回复  更多评论
  
# re: msdn也有错? 2007-09-13 10:11 | 蚂蚁终结者
应该是MSDN的问题,STL的参考书还是The C++ Standard Library比较棒.  回复  更多评论
  
# re: msdn也有错? 2007-09-13 10:47 | 探丫头
呵呵,我也是看C++程序设计语言那本书的时候偶然看到了这个地方,也不是为了专门学stl  回复  更多评论
  
# re: msdn也有错? 2008-01-28 17:31 | 浪迹天涯
可以参考《Effective STL》
条款3:使容器里对象的拷贝操作轻量而正确

容器容纳了对象,但不是你给它们的那个对象。此外,当你从容器中获取一个对象时,你所得到的对象不是容器里的那个对象。取而代之的是,当你向容器中添加一个对象(比如通过insert或push_back等),进入容器的是你指定的对象的拷贝。拷进去,拷出来。这就是STL的方式。
.........  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理