Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4460640
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: C/C++

2008-03-24 15:28:28

C++是提倡用容器取代数组  那么容器的排序问题是如何解决的呢  其实算法都一样  不知道会不会多此一举本人是菜鸟,还望老鸟多多指教
本程序已经做了多种设想  已经具备一些简单的排错功能  还有什么不足之处 望指教一二.

#include <vector>
#include <iostream>
#include <conio.h>

void main (void)
{
using namespace std;
// 创建容器

    vector<int> vctint;
// 输入数据并放入容器中 若为非数字则从容器弹出

cout << "Input the digit num:";
int n;
cin >> n;
  //对输入进行检测 若非数字则提示错误



if(cin.fail())
{
  cin.clear();
  char er;
  cin >> er;
  cout << er << "not correct\n";
  return;
}
  for(int i = 0; i != n; i++)
  {
  int ch;
  cin >> ch;
    vctint.push_back(ch);


if(0 != isdigit(ch))
{
  cout << "not digit drop it!\n";
  vctint.pop_back();
  }

  }


//

for(size_t cmpnum = vctint.size()-1; cmpnum != 0; --cmpnum)
{
  for (size_t i = 0; i != cmpnum; ++i)
  {
  if (vctint[i] > vctint[i+1])
  {
    int temp;
    temp = vctint[i];
    vctint[i] = vctint[i+1];
    vctint[i+1] = temp;
  }
  }
}


// 用迭代器

vector<int>::iterator iter;
cout <<"the sorted digits are:\n";
for(iter = vctint.begin(); iter != vctint.end(); ++iter)
cout << *iter <<" ";
}

 

 

阅读(5890) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2008-04-20 20:18:05

嗯 的确用int有不当之处 谢谢提醒

chinaunix网友2008-04-20 14:42:58

for(int cmpnum = vctint.size()-1; cmpnum != 0; --cmpnum) //换成 size_t 吧