Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

C++ vector of struct allocated on stack

If we have a vector of struct pointer MyInfo* (allocated on heap). Then we can check vec[i] == NULL to know whether there is a struct in the vec[i], like this, if (vec[i] != NULL) //then do some processing

However, if we allocate MyInfo on stack instead of on heap, then we have vector<MyInfo> as shown below. I guess each vec[i] is initialized by the struct default constructor. How do you check whether vec[i] contains a non-empty struct similar to above NULL pointer case, like if (vec[i] contains valid struct) //then do some processing

My code is below

#include <iostream>     // std::cout
#include <string>
#include <vector>

using namespace std;

struct MyInfo {
    string name;
    int age;
};

int main () {
    vector<MyInfo> vec(5);
    cout << "vec.size(): " << vec.size() << endl;
    auto x = vec[0];
    cout << x.name << endl; //this print "" empty string
    cout << x.age << endl; //this print 0

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are some options you can use. The first and easiest one, is to define a value to each (or for one) of your struct's variables, that will point that the struct is not initialized yet. In this case, age should be large or equal to 0, to be logicly straight. So, you can initialize it to -1, like this:

struct MyInfo {
    string name;
    int age = -1;
};
// Or
struct MyInfo {
    string name;
    int age;
    MyInfo() : name(""), age(-1) {} // Use constructor
};

Now, in your main function, it will print in the age the value -1. Also, you can see the empty of the name variable as a sign for it too.

Another way might be using flag and get/set operations to indicate when the variables are initialize:

struct MyInfo {
private:
    std::string _name;
    int _age;
    bool age_initialize = false;
    bool name_initialize = false;

public:
    void name(const std::string &name_p) { _name = name_p; name_initialize = true; }
    void age(int age_p) { _age = age_p; age_initialize = true; }
    void init(int age_p, const std::string &name_p) { age(age_p); name(name_p); }
    bool is_initialize() { return name_initialize && age_initialize; }
    int age() { return _age; }
    std::string name() { return _name; }
};

int main() {
    std::vector<MyInfo> vec(5);
    std::cout << "vec.size(): " << vec.size() << std::endl;

    auto x = vec[0];
    std::cout << x.is_initialize() << std::endl; //this print 0
    std::cout << x.name() << std::endl; //this print "" empty string
    std::cout << x.age() << std::endl; //this print 0

    return 0;
}

You can also throw an exception when calling int age() of std::string name() function, if those values are not initialize yet:

struct MyInfo {
private:
    /* ... */

public:
    /* ... */
    int age() {
        if (!age_initialize) throw std::runtime_error("Please initialize age first.");
        return _age;
    }
    std::string name() {
        if (!name_initialize) throw std::runtime_error("Please initialize name first.");
        return _name;
    }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...