幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2619|回复: 2

[通用编程] 关于向量的问题

[复制链接]

550

主题

9116

帖子

214748万

积分

超级版主

如同神一般的存在,腿神!拖后腿的神~~

Rank: 8Rank: 8

积分
2147483647
发表于 2010-12-21 20:37:31 | 显示全部楼层 |阅读模式
我的是VS08

new project -> General -> Empty Project

我建立一个main.cpp放到工程里面  以下是代码
  1. #include <iostream>
  2. #include <vector>
  3. //要包含这个头文件
  4. using namespace std;
  5. int main(void)
  6. {     
  7.     vector<int> a(10);//定义了10个整数元素的向量,但没有给出初值,因而,其值是不确定滴
  8.     vector<int> b(10,1);//定义了10个证书元素的向量,且给出每个元素的初值为1
  9.     vector<int> c(b); //用另一个现成的向量来创建一个向量
  10.     vector<int> d(b.begin(),b.begin() + 3);//定义了其值依次为b向量中第0到第2个元素的向量
  11.     cout << " 1---   ";
  12.     for (int i=0;i<d.size();i++)
  13.         cout << d << "  "; //使用下标形式,a是向量元素操作,和数组一样
  14.     cout << &#39;\n&#39;;    cout << " 2---   ";
  15.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  16.         cout << *it << "  ";//使用遍历器方式,*it是指针间接形式,它的意思是it所指向的元素值
  17.     cout << &#39;\n&#39;;    cout << " 3---   ";
  18.     a.assign(b.begin(),b.begin()+3); //b向量的0~2元素构成向量赋给a
  19.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  20.         cout << *it << "  ";
  21.     cout << &#39;\n&#39;;    cout << " 4---   ";
  22.     a.assign(8,2); //使a向量中只含0-7元素,且赋值给2
  23.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  24.         cout << *it << "  ";
  25.     cout << &#39;\n&#39;;    cout << " 5---   ";
  26.     int x = a.back();//将a中最后一个元素值赋给整数变量x
  27.     cout << "x = " << x << endl;
  28.     cout << " 6---   ";
  29.     int y = a.front(); //将a中第一个向量元素赋给整数变量y
  30.     cout << "y = " << y << endl;
  31.     cout << " 7---   ";
  32.     a.clear();
  33.     if (a.empty()) cout << "a is empty!" << endl;
  34.     cout << " 8---   ";
  35.     a.resize(5); //将向量元素个数调至5个,多删少补,其值随机
  36.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  37.         cout << *it << "  ";
  38.     cout << &#39;\n&#39;;    cout << " 9---   ";
  39.     a.resize(6,4); //将向量元素个数调至5个,多删少补,其值均为4
  40.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  41.         cout << *it << "  ";
  42.     cout << &#39;\n&#39;;        cout << "10---   ";
  43.     a.push_back(100);//在向量a最后插入一个元素,其值为100
  44.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  45.         cout << *it << "  ";
  46.     cout << &#39;\n&#39;;cout << "11---   ";
  47.     a.pop_back(); //删除a向量最后一个元素
  48.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  49.         cout << *it << "  ";
  50.     for (int i=0;i<a.size();i++)
  51.         a = i;
  52.     cout << &#39;\n&#39;;    cout << "12---   ";
  53.     a.erase(a.begin()+2);
  54.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  55.         cout << *it << "  "; //删除第三个位置的元素
  56.     cout << &#39;\n&#39;;    cout << "13---   ";
  57.     a.insert(a.begin() + 3, 44); //在第四个位置插入一个元素
  58.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  59.         cout << *it << "  ";
  60.     cout << &#39;\n&#39;;    cout << "13---   ";
  61.     a.erase(a.begin()+2,a.begin()+4);//删除第三个位置到第四个位置
  62.     for (vector<int>::iterator it = a.begin();it!=a.end();++it)
  63.         cout << *it << "  ";
  64.     cout << &#39;\n&#39;;    cout << "14---   ";
  65.     if (a==b) //向量的比较操作,操作符还有!=,<,<=,>,>=,
  66.         cout << "a equal b";
  67.     else
  68.         cout << "a not equal b";
  69.     cin.get();
  70.     return 0;
  71. }
复制代码


执行出来的倒是没问题,我的问题是这个
  1. 1>------ Build started: Project: Vector_text, Configuration: Debug Win32 ------
  2. 1>Compiling...
  3. 1>main.cpp
  4. 1>c:\documents and settings\gx\my documents\visual studio 2008\projects\vector_text\vector_text\main.cpp(13) : warning C4018: &#39;<&#39; : signed/unsigned mismatch
  5. 1>c:\documents and settings\gx\my documents\visual studio 2008\projects\vector_text\vector_text\main.cpp(52) : warning C4018: &#39;<&#39; : signed/unsigned mismatch
  6. 1>Linking...
  7. 1>LINK : C:\Documents and Settings\GX\My Documents\Visual Studio 2008\Projects\Vector_text\Debug\Vector_text.exe not found or not built by the last incremental link; performing full link
  8. 1>Embedding manifest...
  9. 1>Build log was saved at "file://c:\Documents and Settings\GX\My Documents\Visual Studio 2008\Projects\Vector_text\Vector_text\Debug\BuildLog.htm"
  10. 1>Vector_text - 0 error(s), 2 warning(s)
  11. ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
复制代码


编译过去了,这个报错是怎么回事? 2 Warning是啥意思。怎么让他不报错??问完了。
报错的是这两个
for (int i=0;i<d.size();i++)  13行
for (int i=0;i<a.size();i++)   52行
我就是你们的神,庶民们,追随我吧!跟着我一起拖后腿!
回复

使用道具 举报

136

主题

1751

帖子

548

积分

版主

Rank: 7Rank: 7Rank: 7

积分
548
发表于 2010-12-22 12:16:24 | 显示全部楼层
这是开/W3才出现的警告。
vector.size()返回的是无符号整数unsigned int
而你在for中声明的i是有符号的int
改成for(unsigned int i = 0; i&lt;a.size(); i++)就没问题了。
え~え~お!!!
回复 支持 反对

使用道具 举报

550

主题

9116

帖子

214748万

积分

超级版主

如同神一般的存在,腿神!拖后腿的神~~

Rank: 8Rank: 8

积分
2147483647
 楼主| 发表于 2010-12-22 16:04:22 | 显示全部楼层
谢谢谢谢,灰常感谢
我就是你们的神,庶民们,追随我吧!跟着我一起拖后腿!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-4-20 21:48 , Processed in 0.019380 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表