| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 396 人关注过本帖
标题:vector 和 数组 排序问题
只看楼主 加入收藏
haibohere
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2013-1-14
结帖率:100%
收藏
 问题点数:0 回复次数:1 
vector 和 数组 排序问题
// 龙马精神.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <vector>
#include <iomanip>
using std::vector;
using std::cout;
using std::cin;
using std::endl;
void paixu(double &a,double &b);
int _tmain(int argc, _TCHAR* argv[])
{
    srand(time(NULL));      //生成随机数
    vector<double> vip(10);    //一个容器
    for(int i=0;i<10;i++)     //生成几个随机数储存到容器中
        vip[i]=rand()%20;

    for(int i=0;i<10;i++)     //把容器中的数字输出到屏幕
        cout<<std::setw(5)<<vip[i];
    cout<<endl;
    for(int i=0;i<9;i++)    //给容器中的数字排序
    for(int i=0;i<9;i++)
        paixu(vip[i],vip[i]);
    for(int i=0;i<10;i++)       //再次输出容器中的数字到屏幕,可是排序没体现出作用,还是第一次数字的顺序。
        cout<<vip[i]<<endl;
    int num=0;
    cin>>num;
    return 0;
}

void paixu(double &a,double &b){    //排序函数
    double c;
    if(a>b)
    {
        c=a;
        a=b;
        b=c;
    }
};

我用的容器,想给数字排序, 可是一点效果都没有,用数组就能。
想问下容器怎么不行。下面是数组的代码 我用的是 vs2010
一言九鼎.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
void px(int &a,int &b);
int _tmain(int argc, _TCHAR* argv[])
{
    srand(time(NULL));
    int num[10];
    for(int i=0;i<10;i++)
    {
        num[i]=rand()%90;
        cout<<std::setw(5)<<num[i];
    }
    cout<<endl;
    for(int i=0;i<9;i++)
    for(int i=0;i<9;i++)
    px(num[i],num[i+1]);
    for(int i=0;i<10;i++)
        cout<<std::setw(5)<<num[i];
    cout<<endl;
    int boy=0;
    cin>>boy;
    return 0;
}
void px(int &a,int &b){
    int c;
    if(a<b)
    {
        c=a;
        a=b;
        b=c;
    }
};


搜索更多相关主题的帖子: vector 应用程序 include double 
2013-02-09 14:44
haibohere
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2013-1-14
收藏
得分:0 
我自己的小失误 丢人了
paixu( vip[i],vip[i]);
改成 paixu(vip[i],vip[i+1]);
就好了。
2013-02-09 15:08
快速回复:vector 和 数组 排序问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013943 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved