字符数组排序和删除重复 [英] Char array sorting and removing duplicates

查看:126
本文介绍了字符数组排序和删除重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些数组操作。
我做的字符数组排序在这里重复和去除。
您的意见是值得欢迎的。还没有做了很多的测试和错误处理,虽然在这里。

 #包括LT&;&stdafx.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;串GT;
使用命名空间std;无效的排序(字符*&安培; ARR)
{
    焦温度;
    的for(int i = 0; I<的strlen(ARR);我++)
    {
        对于(INT J = I + 1; J<的strlen(ARR); J ++)
        {
            如果(ARR [I]>常用3 [J]。)
            {
            TEMP = ARR [I]
            改编[I] = ARR [J]。
            ARR [J] =温度;
            }
        }
    }}
布尔是present(字符* uniqueArr,字符* ARR)
{
    布尔isfound = FALSE;
    的for(int i = 0; I<的strlen(ARR);我++)
    {
    对于(INT J = 0; J< = strlen的(uniqueArr); J ++)
    {
        如果(ARR [I] == uniqueArr [J]。)
        {
        isfound = TRUE;
        返回isfound;
        }
        其他
        isfound = FALSE;
    }
    }    返回isfound;
}字符* removeduplicates(字符*&安培; ARR)
{
    字符* uniqqueArr =的strdup(); //为了使这个字符数组修改
    INT索引= 0;
    布尔DUP = FALSE;
    而(*改编!= NULL)
    {
     DUP =为present(uniqqueArr,ARR);
     如果(DUP ==真)
     {}//没做什么
     否则//复制字符到新的字符数组。
     {
           uniqqueArr [指数] = *编曲;
     指数++;
     }
    ARR ++;
    }
    返回uniqqueArr;
}
诠释的main()
{
    字符* ARR =的strdup(saaangeetha);
    //如果未使用的strdup(),访问冲突写
          //位置在改编[I] = ARR [J]发生。
    //这使得常量字符串修改
    排序(ARR);
    字符* uniqueArr = removeduplicates(ARR);}


解决方案

如果您使用的std ::字符串,您的code(实际上是C风格)可以用C ++编写风格,在短短几行:

 的#include<&iostream的GT;
#包括LT&;串GT;
#包括LT&;&算法GT;诠释主(){
        性病::字符串s =saaangeetha;
        的std ::排序(s.begin(),s.end());
        标准::字符串:迭代它=的std ::唯一的(s.begin(),s.end());
        s.resize(它 - s.begin());
        性病::法院LT&;< S;
        返回0;
}

输出:(所有重复删除)

  aeghnst

演示: http://ideone.com/pHpPh

如果你想的char * 结尾,那么你可以这样做:

 为const char * uniqueChars = s.c_str(); //删除重复项后!

I am trying to do some array manipulations. I am doing char array sorting and duplicates removal here. Your comments are welcome. Havent done much testing and error handling here though.

#include<stdafx.h>
#include<stdlib.h>
#include<stdio.h>
#include<string>
using namespace std;

void sort(char *& arr)
{
    char temp;
    for(int i=0;i<strlen(arr);i++)
    {
        for(int j=i+1;j<strlen(arr);j++)
        {
            if(arr[i] > arr[j])
            {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            }
        }
    }

}
bool ispresent(char *uniqueArr, char * arr)
{
    bool isfound = false;
    for(int i=0;i<strlen(arr);i++)
    {
    for(int j=0;j<=strlen(uniqueArr);j++)
    {
        if(arr[i]== uniqueArr[j])
        {
        isfound = true;
        return isfound;
        }
        else
        isfound = false;
    }
    }

    return isfound;
}

char * removeduplicates(char *&arr)
{
    char * uniqqueArr = strdup(""); // To make this char array modifiable
    int index = 0;
    bool dup = false;
    while(*arr!=NULL)
    {       
     dup = ispresent(uniqqueArr, arr);
     if(dup == true)
     {}//do nothing
     else// copy the char to new char array.
     {
           uniqqueArr[index] = *arr;    
     index++;
     }
    arr++;
    }
    return uniqqueArr;
}
int main()
{
    char *arr = strdup("saaangeetha"); 
    // if strdup() is not used , access violation writing to 
          //location occurs at arr[i] = arr[j]. 
    //This makes the constant string modifiable
    sort(arr);
    char * uniqueArr = removeduplicates(arr);   

}

解决方案

If you use std::string, your code (which is actually C-Style) can be written in C++ Style in just these lines:

#include <iostream>
#include <string>
#include <algorithm>

int main() {
        std::string s= "saaangeetha";
        std::sort(s.begin(), s.end());
        std::string::iterator it = std::unique (s.begin(), s.end()); 
        s.resize( it - s.begin());
        std::cout << s ;
        return 0;
}

Output: (all duplicates removed)

aeghnst

Demo : http://ideone.com/pHpPh

If you want char* at the end, then you can do this:

   const char *uniqueChars = s.c_str(); //after removing the duplicates!

这篇关于字符数组排序和删除重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆