afisare vector fara bucla

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
User avatar
Sanda X
Membru
Membru
Posts: 101
Joined: 14 Jul 2007, 21:50

afisare vector fara bucla

Post by Sanda X » 20 Aug 2009, 18:04

Am o functie care afiseaza la consola stringurile dintr-un vector.

Code: Select all

void afiseaza_vector(vector<string>& v)
{
    int n = v.size();
    for(int i = 0; i < n; i++)
            cout << v[i] << endl;
}
Aceeasi functie scrisa mai elegant cu iteratori

Code: Select all

void afiseaza_vector(vector<string>& v)
{
    vector<string>::const_iterator e = v.end();
    for(vector<string>::iterator i = v.begin(); i < e; ++i)
            cout << *i << endl;
}
As vrea sa o scriu inca si mai frumos fara sa folosesc bucla for sau while.
Se poate?


P.S. Am impresia ca am vazut pe aici sau in alt forum dar acum nu mai gasesc.


Sanda X

Dragos Cojocari
Membru++
Membru++
Posts: 789
Joined: 11 Jul 2007, 14:11

Re: afisare vector fara bucla

Post by Dragos Cojocari » 20 Aug 2009, 18:25

Code: Select all

std::copy( v.begin(), v.end(), std::ostream_iterator<std::string>( std::cout, " " ));

User avatar
Parametru
Junior
Junior
Posts: 6
Joined: 15 Jul 2012, 00:04
Judet: Buzău

Re: afisare vector fara bucla

Post by Parametru » 15 Jul 2012, 17:08

Code: Select all

#include <iostream>
#include <algorithm>
#include <vector>

vector <string> siruri; //sa zicem ca e plin de strings

void buffer(string i)
{
     std::cout << i << std::endl;
}

int main()
{
     for_each(siruri.begin(), siruri.end(), buffer);
     return 0;
}

Post Reply