Do template classes inherit class members passed to them? (Definitely std :: vector)

I have a question regarding vectors:

If I have std :: vector <MyClass> will this vector "inherit" the functions of the MyClass member or not? If not, then would there be a better way to individually process MyClass elements inside a loop? Should I create a new class object at each iteration and assign it the current vector iterator?

class MyClass
{
    public:
        void setMembers(std::string &string, int &num)
        {
            mystring = string;
            number = num;
        }
        string printString() { return mystring; }
        int printNumber() { return number; }

    private:
        std::string mystring;
        int number;
};

    MyClass test;
    std::vector<MyClass> vector;

    std::string string = "Test 1";
    int a = 3;

    test.setMembers(string,a);
    vector.push_back(test);

    for(unsigned int i = 0; i<vector.size(); i++) {
        cout << "Vector #" <<  << endl;
        // does vector inherit member functions here?
        cout << "mystring is: " << vector.printString()<< endl;
        cout << "number is  : " << vector.printNumber() << endl;
    }

Thank you very much for your help.

+3
source share
10 answers

No, an instance of std :: vector does not inherit your member variables. However, the objects in the vector have those elements that you can access through the [] operator.

for (size_t i = 0; i < vector.size(); i++) {
    cout << "Vector #" << i << endl;
    cout << "mystring is: " << vector[i].printString() << endl;
    cout << "number is  : " << vector[i].printNumber() << endl;
}

, [i], MyClass, - printString() printNumber().

.

+4

. . , .

. () - for, :

for(size_t i = 0; i<vector.size(); i++) { // technically, you should use size_t here, since that is the type returned by vector.size()
    cout << "Element #" <<  << endl; // We're iterating through the elements contained in the vector, so printing "Vector #" doesn't make sense. There is only one vector
    cout << "mystring is: " << vector[i].printString()<< endl; // [i] to access the i'th element contained in the vector
    cout << "number is  : " << vector[i].printNumber() << endl;
}

- , . . -, . , . vector[i].printString() printString() . ( iter, iter->printString())

, . , , deques , , , , , .

, , , :

forstd::vector<MyClass> current = vector.begin(); current != vector.end(); ++current) {
    cout << "mystring is: " << current->printString() << endl;
    cout << "number is  : " << current->printNumber() << endl;
}

, i, , . begin/end , , . , , , , . , .

, , . ++ . <algorithm>.

- std::for_each, for. , , , , , . , , :

void Print(const MyClass& obj) {
    cout << "mystring is: " << obj.printString() << endl;
    cout << "number is  : " << obj.printNumber() << endl;
}

. , , . for_each:

std::for_each(vector.begin(), vector.end(), Print);

, . , .

, . :

std::for_each(vector.begin() + 5, vector.end(), Print);

:

std::for_each(vector.begin(), vector.begin()+3, Print);

, . , ( ):

std::copy(vector.begin(), vector.end(), dest.begin());

dest , , . std:: cout, ( , MyClass operator <<, .)

std:: cout, std::transform, , . MyClass objec, , :

std::string ToString(const MyClass& obj) {
  return std::string("mystring is: " + obj.printString() + "\nnumber is  :" << obj.printNumber() + "\n";
}

, . , MyClass, . std:: cout:

std::transform(vector.begin(), vector.end(), std::ostream_iterator(std::cout), ToString);

std::ostream_iterator std::cout, . , " " . , , .

, for , . .

, , . , "" ++.

+4

, .

, std::vector<MyClass> v;, (, .push_back()).

MyClass - :

for (int i = 0; i < v.length(); ++v)
   v[i].printString();

for (std::vector<MyClass>::const_iterator i = v.begin(); i != v.end(); ++i)
   i->PrintString();
+3

" /" "MyClass", "MyClass" ( 0 MyClass), , MyClass,

, , , ( MyClass)

for(unsigned int i = 0; i<vector.size(); i++) {
    cout << "Vector #" <<  << endl;
    // does vector inherit member functions here?
    cout << "mystring is: " << vector[i].printString()<< endl;
    cout << "number is  : " << vector[i].printNumber() << endl;
}

, "" , .

for(std::vector<MyClass>::iterator i = vector.begin(); i != vector.end(); ++i) {
    cout << "Vector #" <<  << endl;
    // does vector inherit member functions here?
    cout << "mystring is: " << i->printString()<< endl;
    cout << "number is  : " << i->printNumber() << endl;
}

, .

+3

. - . [] ()() . .

.

V [I].printString();

v.at().pringString();

+2

, . - , :

for(vector<MyClass>::iterator i=vector.begin();i!=vector.end();i++) {
    cout << "mystring is: " << i->printString() << endl;
    cout << "number is  : " << i->printNumber() << endl;
}
+2

, . :

cout << "Vector #" <<  << endl;
for( unsigned int i = 0; i <vector.size(); i++) {
    cout << "mystring at " << i << " is "  << vector[i].printString()<< endl;
    cout << "number at " << i << " is " << vector[i].printNumber() << endl;
}
+1

. ; , , , .

+1

, std::vector<MyClass> MyClass, STL , .
:

#include <vector>
#include <algorithm>
#include <boost/bind.hpp>

struct Bla
{
    Bla(int i = 0) : m_i(i) {}

    void print() { printf("%d ", m_i); }
    void printAdd(int a) { printf("%d ", m_i +  a); }

    Bla add(int a) { return Bla(m_i + a); }
    int geti() { return m_i; }

    int m_i;
};

void printInt(int i)
{
    printf("%d ", i);
}

int main(int argc, char *argv[])
{
    std::vector<Bla> bla;
    bla.push_back(Bla(1));
    bla.push_back(Bla(2));

    // print the elements in the vector
    std::for_each(bla.begin(), bla.end(), boost::mem_fn(&Bla::print));
    printf("\n");
    // a complex operation on the vector requiring an additional argument for the call
    std::for_each(bla.begin(), bla.end(), boost::bind(&Bla::printAdd, _1, 10));
    printf("\n");

    // extract a single member from the vector into a second vector
    std::vector<int> result;
    result.resize(bla.size());
    std::transform(bla.begin(), bla.end(), result.begin(), boost::bind(&Bla::geti, _1));
    // print the result
    std::for_each(result.begin(), result.end(), &printInt);
    printf("\n");

    // transform the vector into a different vector using a complex function that requires an argument.
    std::vector<Bla> result2;
    result2.resize(bla.size());
    std::transform(bla.begin(), bla.end(), result2.begin(), boost::bind(&Bla::add, _1, 10));
    std::for_each(result2.begin(), result2.end(), boost::mem_fn(&Bla::print));
    printf("\n");

    return 0;
}
+1

! , , .

, , .

, . !

0
source

Source: https://habr.com/ru/post/1706534/


All Articles