当前位置:首页
开发技术指南» 文章正文
    引言:

 ·有关规则表达式的问题::    »显示摘要«
    摘要: 验证短时间的函数,形如 (13:04:06) function istime(str) { var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/); if (a == null) {alert(输入的参数不是时间格式); return false;} if (a[1]>24 || a[3]>60 || a[4]&g......
 ·怎么在“textarea”输出时换行    »显示摘要«
    摘要: 如题。 ......


有关vector

对于给vector中的元素赋值,可以用静态数组直接赋值,也可以插值,但除此之外,如何一个一个元素赋值?请赐教相关算法,谢谢。

NO.1   作者: dddd8888

#include   <vector>  
  #include   <iostream>  
  using   namespace   std;  
   
   
  int   main(int   argc,   char*   argv[])  
  {  
  vector<int>   v(10);  
   
  for(int   i=0;   i<10;   ++i)  
  {  
  v[i]   =   i;  
  }  
   
  if   (v.size()   ==   v.capacity())  
  {  
  v.resize(15);  
  }  
   
  v[10]   =   10;  
   
  return   0;  
  }

NO.2   作者: dnawym

第一个方法使用索引  
  第二个方法使用迭代器  
   
  #include   <iostream>  
  #include   <vector>  
  #include   <iterator>  
   
  using   std::cout;  
  using   std::endl;  
  using   std::vector;  
   
  int   main()  
  {  
          vector<int>   v(10);  
          int   j;  
  //   用索引          
          j   =   0;  
          typedef   vector<int>::size_type   vec_sz;  
          for   (vec_sz   i   =   0;   i   <   v.size();   ++i,   ++j)  
          {  
                  v[i]   =   j;  
          }  
          for   (vec_sz   i   =   0;   i   <   v.size();   ++i)  
          {  
                  cout   <<   v[i]   <<   "   ";  
          }  
          cout   <<   endl;  
   
  //   用迭代器          
          j   =   0;  
          typedef   vector<int>::iterator   iter;  
          for   (iter   i   =   v.begin();   i   !=   v.end();   ++i,   ++j)  
          {  
                  *i   =   j;  
          }  
          typedef   vector<int>::const_iterator   C_iter;  
          for   (C_iter   i   =   v.begin();   i   !=   v.end();   ++i)  
          {  
                  cout   <<   *i   <<   "   ";  
          }  
          cout   <<   endl;  
           
          system("pause");  
          return   0;  
  }

NO.3   作者: Wolf0403

对于一个数组   int   arr[5];   有两种访问方法:  
  arr[3]   =   3;   通过   operator[]   访问  
  int   *   p   =   &arr[0];  
  *(p   +   3)   =   3;   通过指向数组的指针访问。  
   
  std::vector   重载了   operator[]   支持类似数组的第一种赋值方法  
  同时实现了   iterator   支持类似指针的第二种访问方法。  
   
  楼上的代码提示得很清楚了


 ·求vod源码    »显示摘要«
    摘要: 小弟需要一个vod的源码,各位大哥大姐能否提供一个给我呀,感激不尽。谢谢 ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE