Home > Back-end >  C Create an object with array simultaneously with the constructor
C Create an object with array simultaneously with the constructor

Time:09-22

So, I've been exploring on how to create a dynamical array with a custom template class that I made.

#include <iostream>
#include <vector>

//HOW TO SET CLASS INTO DYNAMICAL ARRAY WITH VECTOR


//CREATE A CLASS
class User{
    std::string name;
public:
    User(){
    }
    User(std::string name){
        this->name = name;
    }
    void set_name(std::string name){
        this->name = name;
    }
    std::string get_name(){
        return name;
    }
};

int main(){
    //SET A NORMAL ARRAY THAT CAN CONTAIN AN OBJECT
    User user[1];

    //DO WHATEVER WITH THE USER[0] TO SET EVERYTHING THAT LATER WILL BE PUT IN VECTOR
    user[0].set_name("Meilianto");
    std::cout << "user[0]: " << user[0].get_name() << std::endl;

    //CREATE A DYNAMICAL ARRAY WHICH IS VECTOR
    std::vector<User> vuser;

    //PUSHBACK TO THE VECTOR AS "FIRST ELEMENT" BY PUTTING "USER[0]" AS AN ARGUMENT
    vuser.push_back(user[0]);
    std::cout << "vuser[0]: " << vuser[0].get_name() << std::endl;

    //YOU CAN "MODIFIED" THE "USER[0]" AND ADD AGAIN AS THE "SECOND ELEMENT" OF VECTOR
    user[0].set_name("Meilianto1");
    vuser.push_back(user[0]);
    std::cout << "vuser[1]: " << vuser[1].get_name() << std::endl;

    //YOU CAN EVEN "MODIFIED" THE "FIRST ELEMENT" BY CALLING THE "METHOD" OF IT
    vuser[0].set_name("Hantu");
    std::cout << "vuser[0]: " << vuser[0].get_name() << std::endl;

    //THE QUESTION HERE, CAN I DECLARE ARRAY TOGETHER WITH THE CONSTRUCTOR?
    User user1[1]("Bebek");
    
    //AND AFTER THAT I CAN ADD THAT OBJECT STRAIGHT AWAY TO VECTOR WITHOUT ASSIGNING ALL THE 
    //MEMBERS ONE BY ONE

    return 0;
}

If you have read my comments in my code, what I am trying to do is maybe it will be faster if I just construct right away when I create the object instead of assigning all the members one by one that will cost more code. I imagine if in the future there will be an object with a lot of members and need to assign it one by one. It won't be efficient.

EDIT: I edit the User user[0] into User user[1], Thanks

CodePudding user response:

If you're using a modern standard of C , then you can do this

std::vector<User> vuser {
 {"Meilianto1"}, 
 {"Hantu"},
 {"Bebek"}
};

Where each pair of inner brackets with a string calls User constructor, and outer pair of brackets calls std::vector<User> constructor with a sequence of Users

CodePudding user response:

THE QUESTION HERE, CAN I DECLARE ARRAY TOGETHER WITH THE CONSTRUCTOR ?

User user1[1]("Bebek");

You can use list initialization for that, for arrays as well as for vectors:

User users[] { std::string("Herbert"), std::string("Anton") };
std::vector<User> vusers { std::string("Herbert"), std::string("Anton") };

CAN I ADD THAT OBJECT STRAIGHT AWAY TO VECTOR WITHOUT ASSIGNING ALL THE MEMBERS ONE BY ONE

You can initialize a vector with the elements of an previously defined array like this:

std::vector<User> v2users(std::cbegin(users), std::cend(users));

BTW: note that User user[0]; in your code defines an array without elements, i.e. of size 0, which rarely makes sense. Accessing user[0] leads to undefined behaviour

CodePudding user response:

Yes, you can!

Like this:

User users[]{ User{ "one" }, User{ "two" } };

// Construct vector from iterator-pair:
std::vector<User> users_vector{ std::cbegin(users), std::cend(users) }; 

CodePudding user response:

You can use emplace_back method or push_back with temporary object to add the vector. For example;

vuser.emplace_back("Meilianto1"); // 1 
vuser.push_back(User{"Meilianto1"}); // 2

The first one passes the arguments to the constructor. The second one will move the variable to vector. To be honest, there will be copy elision that's why there will be no move or copy. Just it will construct the variable.

  •  Tags:  
  • c
  • Related