example3_usage_with_multiple_arguments.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
#include <string>

struct ComplexObject {
    std::string name;
    int id;
    double value;
    
    ComplexObject(std::string n, int i, double v) 
        : name(std::move(n)), id(i), value(v) {
        std::cout << "Constructing ComplexObject: " << name << std::endl;
    }
    
    friend std::ostream& operator<<(std::ostream& os, const ComplexObject& obj) {
        return os << obj.name << " (ID: " << obj.id << ", Value: " << obj.value << ")";
    }
};

int main() {
    std::vector<ComplexObject> objects;
    
    objects.emplace_back("Object1", 1, 3.14);
    objects.emplace_back("Object2", 2, 2.718);
    
    for (const auto& obj : objects) {
        std::cout << obj << std::endl;
    }
    
    return 0;
}
Back to emplace_back