vijayan121 1,152 Posting Virtuoso

Do you can show me a non-vector solution.

Hmm.. Thesnippet I posted dores not use vectors; instead it uses C-style null-terminated arrays of char (and dynamically allocated memory). With annotatations:

// input: an array of char terminated by a null-character
// returns: a dynamically allocated array of unique chars in the input
//          terminated by a null-character

char* remove_duplicates_preserve_order( const char* cstr )
{
    // 1. get a count of the number of unique chars in the input array
    // ---------------------------------------------------------------

    bool already_seen[UCHAR_MAX] = { false } ; // to look up already seen chars 
    int cnt_unique = 0 ; // number of unique chars in the string

    // for every char in the input array till we encounter the null-character
    for( const char* p = cstr ; *p ; ++p ) 
    {
        const unsigned char u = *p ;

        // if we have not seen this char earlier, increment cnt_unique 
        // and set already_seen[u] to true
        if( !already_seen[u] ) { already_seen[u] = true ; ++cnt_unique ; }
    }


    // 2. allocate memory for the result (a null-terminated array of chars)
    // --------------------------------------------------------------------

    char* result = new char[ cnt_unique + 1 ] ; // + 1 for the null-termination 
    result[cnt_unique] = 0 ; // null-terminate result


    // 3. copy unique chars in the input array to the result array
    // -----------------------------------------------------------

    // look up table to check if we had alredy copied a char to the result
    bool already_copied[UCHAR_MAX] = { false } ;

    // for every …
WaltP commented: Nooooob, remember? Dynamic memory? C'mon!!! -3
vijayan121 1,152 Posting Virtuoso

Use std::sort() and std::unique() in <algorithm>

#include <algorithm>
#include <iostream>

int main()
{
    int a[] = { 0, 1, 0, 2, 3, 4, 3, 3, 5, 2 } ;
    enum { SIZE = sizeof(a) / sizeof(*a) } ;
    std::sort( a, a+SIZE ) ;
    const int size_unique = std::unique( a, a+SIZE ) - a ;
    std::cout << "size_unique: " << size_unique << '\n' ; // size_unique: 6
    for( int i=0 ; i<size_unique ; ++i ) std::cout << a[i] << ' ' ; // 0 1 2 3 4 5 
}
WaltP commented: You learn nothing from using language extensions. -3
m4ster_r0shi commented: I don't see any language extensions here. This code is standard C++. +6
vijayan121 1,152 Posting Virtuoso
WaltP commented: What is it with BOOST? Is it the cure-all of all problems? -4
vijayan121 1,152 Posting Virtuoso

a sequence container can also be initialized with a sequence identified by a pair of iterators.

char cstr[] = "abcdefghijkl" ;
  std::list<int> list2( cstr, cstr+sizeof(cstr) ) ;

in c++0x, the following would be another way:

std::list<int> list5 = { 23, 6, 57, 575, 7575, 75 }; // like aggreagate initializer in C
iamthwee commented: This is c++? -2