Domanda

È possibile creare un contenitore simile a STL, o anche solo un iteratore in stile STL, per una matrice esistente di elementi di tipo POD?

Ad esempio, supponiamo che io abbia una matrice di ints. Sarebbe conveniente poter chiamare alcune delle funzioni STL, come find_if, count_if o ordinare direttamente su questo array.

Non soluzione: copia dell'intero array o anche solo riferimenti agli elementi. L'obiettivo è quello di risparmiare molto tempo e memoria, sperando nel contempo di utilizzare altri algoritmi STL.

È stato utile?

Soluzione

Puoi chiamare molti degli algoritmi STL direttamente su un normale array in stile C: sono stati progettati per funzionare. . Per esempio,:

int ary[100];
// init ...

std::sort(ary, ary+100); // sorts the array
std::find(ary, ary+100, pred); find some element

Penso che scoprirai che la maggior parte delle cose funziona esattamente come ti aspetteresti.

Altri suggerimenti

È possibile utilizzare un modello di funzione inline in modo da non dover duplicare l'indice dell'array

template <typename T, int I>
inline T * array_begin (T (&t)[I])
{
  return t;
}

template <typename T, int I>
inline T * array_end (T (&t)[I])
{
  return t + I;
}

void foo ()
{
  int array[100];
  std::find (array_begin (array)
      , array_end (array)
      , 10);
}

Tutti gli algoritmi STL utilizzano iteratori.
Un puntatore è un iteratore valido in una matrice di oggetti.

N.B. L'iteratore finale deve essere un elemento oltre la fine dell'array. Da qui i dati + 5 nel seguente codice.

#include <algorithm>
#include <iostream>
#include <iterator>

int main()
{
    int   data[] = {4,3,7,5,8};
    std::sort(data,data+5);

    std::copy(data,data+5,std::ostream_iterator<int>(std::cout,"\t"));
}

Puoi utilizzare Boost.Array per creare un tipo di array C ++ con semantica STL.

utilizzando array:

int a[100];
for (int i = 0; i < 100; ++i)
    a[i] = 0;

utilizzando boost.arrays:

boost::array<int,100> a;
for (boost::array<int,100>::iterator i = a.begin(); i != a.end(); ++i)
    *i = 0;

Aggiornamento: con C ++ 11, ora puoi usare std :: array .

Un puntatore è un modello valido di un iteratore:

struct Bob
{ int val; };

bool operator<(const Bob& lhs, const Bob& rhs)
{ return lhs.val < rhs.val; }

// let's do a reverse sort
bool pred(const Bob& lhs, const Bob& rhs)
{ return lhs.val > rhs.val; }

bool isBobNumberTwo(const Bob& bob) { return bob.val == 2; }

int main()
{
    Bob bobs[4]; // ok, so we have 4 bobs!
    const size_t size = sizeof(bobs)/sizeof(Bob);
    bobs[0].val = 1; bobs[1].val = 4; bobs[2].val = 2; bobs[3].val = 3;

    // sort using std::less<Bob> wich uses operator <
    std::sort(bobs, bobs + size);
    std::cout << bobs[0].val << std::endl;
    std::cout << bobs[1].val << std::endl;
    std::cout << bobs[2].val << std::endl;
    std::cout << bobs[3].val << std::endl;

    // sort using pred
    std::sort(bobs, bobs + size, pred);
    std::cout << bobs[0].val << std::endl;
    std::cout << bobs[1].val << std::endl;
    std::cout << bobs[2].val << std::endl;
    std::cout << bobs[3].val << std::endl;

    //Let's find Bob number 2
    Bob* bob = std::find_if(bobs, bobs + size, isBobNumberTwo);
    if (bob->val == 2)
        std::cout << "Ok, found the right one!\n";
    else 
        std::cout << "Whoops!\n";

    return 0;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top