C++ Struct
1. 定义
- A composite type which aggregate multiple fundamental types into a single structure
- it is a new type, defined by user
struct [type_name]
{
member_type1 member_name 1;
member_type2 member_name 2;
member_type3 member_name 3;
...
} [object_names];
2.使用
-
to access a member of a struct we use the
.
operator
object_name.member_name;
-
Examples
example 1
struct ComplexNumber
{
double realPart;
double imagPart;
};
// declare a variable of type ComplexNumber
ComplexNumber x; // x is the object_name
// initialize the data members of x;
x.realPart = 3.0;
x.imagPart = - 2.2;
example 2
struct ComplexNumber
{
double realPart;
double imagPart;
};
// declare a variable of type ComplexNumber
// and initialize using an initializer list
ComplexNumber x = {1.0, 2.3}
example 3
//define the new type ComplexNumber and declare two variables
// x and y of type ComplexNumber
struct ComplexNumber
{
double realPart;
double imagPart;
} x, y;
//initialize x and y
x.realPart = 3.0;
x.imagPart = - 2.2;
y = x; // copy the entire structure y into x
example 4
//define a new anonymous type and declare a variable
// x with this type
struct
{
double realPart;
double imagPart;
} x;
//initialize x and y
x.realPart = 3.0;
x.imagPart = - 2.2;
3. Pointers to struct
- Like any other type, structures can be pointed to by its own type of pointers
-
To access a member of a pointer to struct we use the
->
opterator
object_pointer_name -> member_name;
-
Examples
example 1
//define the new type ComplexNumber
struct ComplexNumber
{
double realPart;
double imagPart;
};
// declare a variable of type complexNumber
complexNumber x = {1.0, 1.0};
complexNumber *ptr = &x; // a pointer to x
// print the data members of y
cout << ptr->realPart << "," << ptr->imagPart << endl;
example 2
struct TreeNode
{
int val;
TreeNode *left; //member_type:TreeNode*, member_name left;
TreeNode *right;
TreeNode(int x): val(x), left(NULL), right(NULL) {} //constructor
};
// initialized by constructor
// val = x, left -> NULL, right -> NULL
// there is no object_name
TreeNode *root; // root is object_pointer_name
auto p = root;
while (p) p = p -> left;
example 3
class Trie {
public:
struct Node
{
bool is_end; //member_type:bool, member_name is_end;
Node *son[26]; // member_type:Node*, member_name son;
// default constructor
Node()
{
is_end = false;
for (int i = 0; i < 26; i ++ ) son[i] = NULL;
}
} *root; //object_name or object_pointer_name?
/** Initialize your data structure here. */
Trie() {
root = new Node();
}
void insert(string word)
{
auto *p = root;
for (auto c : word)
{
int u = c - 'a';
if (p->son[u] == NULL) p->son[u] = new Node();
p = p->son[u];
}
p->is_end = true;
}
};
4. struct dynamic allocation
- struct variables can be allocated dynamically with new and delete
- we can also create arrays of stuct
- Examples
example 1
//define the new type ComplexNumber
struct ComplexNumber
{
double realPart;
double imagPart;
};
// declare a pointer variable of type ComplexNumber
complexNumber *ptr = NULL;
ptr = new ComplexNumber; // allocate memory
//initialize x and y
ptr -> realPart = 3.0;
ptr -> imagPart = - 2.2;
// print the data members of y
cout << ptr->realPart << "," << ptr->imagPart << endl;
delete ptr; // de-allocate memory
example 2
//define the new type ComplexNumber
struct ComplexNumber
{
double realPart;
double imagPart;
};
// declare a pointer variable of type ComplexNumber
complexNumber *ptr = NULL;
ptr = new ComplexNumber[2]; // allocate memory for an array of size 2
//initialize x and y
ptr[0].realPart = 3.0; // note we are using '.' not '->'
ptr[0].imagPart = - 2.2;
ptr[1] = ptr[0];
// print the data members of y
cout << ptr->realPart << "," << ptr->imagPart << endl;
delete[] ptr; // de-allocate memory