3,881
社区成员
发帖
与我相关
我的任务
分享
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
//
typedef struct {
int index; //下标
int value; //值
}Data;
bool operator==(const Data& lhs, const Data& rhs){
return lhs.value == rhs.value;
}
bool operator <(const Data& lhs, const Data& rhs){
return lhs.value < rhs.value;
}
//判断数组中是否有相等值,数组应该已经排好序
bool HasSameValue(const vector <Data>& src){
if (adjacent_find(src.begin(), src.end()) != src.end()){
cout < <"has same value." < <endl;
return true;
}
else
{
cout < <"no same value." < <endl;
return false;
}
}
bool IsGreaterIndex(const Data& lhs, const Data& rhs){
return (lhs.value == rhs.value
&& lhs.index > rhs.index);
}
//判断等值元素中是否有下标递减的情况,
bool HasReverseIndex(const vector <Data>& src){
if (adjacent_find(src.begin(), src.end(), IsGreaterIndex) != src.end()){
cout < <"has great index." < <endl;
return true;
}
else
{
cout < <"no great index." < <endl;
return false;
}
}
const int NUMBER = 100;
const int SIZE=10;
const int RANNUM = 4;
void ResetData(vector <Data>& src){
int i=0;
vector <int> ran(RANNUM);
for(i=0; i <RANNUM; ++i){
ran[i] = rand()%NUMBER;
}
for(i=0; i <SIZE; ++i){
src[i].index = i;
src[i].value = ran[rand()%RANNUM];
}
}
void Print(const vector <Data>& src){
cout < <'{';
int i=0;
for(i=0; i <SIZE; ++i) {
cout < <'(' < <src[i].value < <',' < <src[i].index < <"),";
if ((i+1)%5==0)
cout < <" } " < <endl;
}
}
int main(int argc ,char* argv[]){
srand(static_cast <unsigned int>(time(NULL)));
vector <Data> src(SIZE), temp(SIZE);
do{
ResetData(src);
cout < <"src: " < <endl;
Print(src);
temp = src;
sort(src.begin(), src.end());
cout < <"after sort: " < <endl;
Print(src);
}while(!HasSameValue(src) ¦ ¦ !HasReverseIndex(src));
return 0;
}
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
//
typedef struct {
int index; //下标
int value; //值
}Data;
bool operator==(const Data& lhs, const Data& rhs){
return lhs.value == rhs.value;
}
bool operator <(const Data& lhs, const Data& rhs){
return lhs.value < rhs.value;
}
//判断数组中是否有相等值,数组应该已经排好序
bool HasSameValue(const vector <Data>& src){
if (adjacent_find(src.begin(), src.end()) != src.end()){
cout < <"has same value." < <endl;
return true;
}
else
{
cout < <"no same value." < <endl;
return false;
}
}
bool IsGreaterIndex(const Data& lhs, const Data& rhs){
return (lhs.value == rhs.value
&& lhs.index > rhs.index);
}
//判断等值元素中是否有下标递减的情况,
bool HasReverseIndex(const vector <Data>& src){
if (adjacent_find(src.begin(), src.end(), IsGreaterIndex) != src.end()){
cout < <"has great index." < <endl;
return true;
}
else
{
cout < <"no great index." < <endl;
return false;
}
}
const int NUMBER = 100;
const int SIZE=10;
const int RANNUM = 4;
void ResetData(vector <Data>& src){
int i=0;
vector <int> ran(RANNUM);
for(i=0; i <RANNUM; ++i){
ran[i] = rand()%NUMBER;
}
for(i=0; i <SIZE; ++i){
src[i].index = i;
src[i].value = ran[rand()%RANNUM];
}
}
void Print(const vector <Data>& src){
cout < <'{';
int i=0;
for(i=0; i <SIZE; ++i) {
cout < <'(' < <src[i].value < <',' < <src[i].index < <"),";
if ((i+1)%5==0)
cout < <" } " < <endl;
}
}
int main(int argc ,char* argv[]){
srand(static_cast <unsigned int>(time(NULL)));
vector <Data> src(SIZE), temp(SIZE);
do{
ResetData(src);
cout < <"src: " < <endl;
Print(src);
temp = src;
sort(src.begin(), src.end());
cout < <"after sort: " < <endl;
Print(src);
}while(!HasSameValue(src) ¦ ¦ !HasReverseIndex(src));
return 0;
}
gcc 3.4.4
始终运行,无法停止