View RSS Feed

Maha

Data Structures

Rate this Entry
by , 03-26-2013 at 10:40 AM (1156 Views)
Data Structures
Using the groups of sequential data can be restrictive in C++. To avoid the sequential data there is an introduction of data structures
Data structures
A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. Data structures are declared in C++ using the following syntax:
Code:
struct structname {

Datatype1 member1;
Datatype2 member2;
Datatype3 member3;
so on..
} nameStr;
where structname is a name for the structure type, object_name can be a set of valid identifiers for objects that have the type of this structure. Within braces { } there is a list with the data members, each one is specified with a type and a valid identifier as its name.

The first thing we have to know is that a data structure creates a new type: Once a data structure is declared, a new type with the identifier specified as structname is created and can be used in the rest of the program as if it was any other type. For example:
Code:
struct myStr {
  int val1;
  float val2;
} ;

myStr mem1;
myStr mem2, mem3;
We have first declared a structure type called myStr with two members: val1 and val2, each of a different fundamental type. We have then used this name of the structure type (myStr) to declare three objects of that type: mem1, mem2 and mem3 as we would have done with any fundamental data type.

Once declared, myStr has become a new valid type name like the fundamental ones int, char or short and from that point on we are able to declare objects (variables) of this compound new type, like we have done with mem1, mem2 and mem3.

Right at the end of the struct declaration, and before the ending semicolon, we can use the optional field object_name to directly declare objects of the structure type. For example, we can also declare the structure objects mem1, mem2 and mem3 at the moment we define the data structure type this way:
Code:
struct myStr {
  int val1;
  float val2;
} mem1, mem2, mem3;
It is important to clearly differentiate between what is the structure type name, and what is an object (variable) that has this structure type. We can instantiate many objects (i.e. variables, like mem1, mem2 and mem3) from a single structure type (myStr).

Once we have declared our three objects of a determined structure type (mem1, mem2 and mem3) we can operate directly with their members. To do that we use a dot (.) inserted between the object name and the member name. For example, we could operate with any of these elements as if they were standard variables of their respective types:
Code:
mem1.val1
mem1.val2
mem2.val1
mem2.val2
mem3.val1
mem3.val2
Each one of these has the data type corresponding to the member they refer to: mem1.val1, mem2.val1 and mem3.val1 are of type int, while mem1.val2, mem2.val2 and mem3.val2 are of type float.

Let's see a real example where you can see how a structure type can be used in the same way as fundamental types:
Simple example for structures
Code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct myMvs_t {
  string title;
  int year;
} mine, yours;

void printmyMv (myMvs_t myMv);

int main ()
{
  string mystr;

  mine.title = "TEST MISSION";
  mine.year = 1940;

  cout << "Enter title: ";
  getline (cin,yours.title);
  cout << "Enter year: ";
  getline (cin,mystr);
  stringstream(mystr) >> yours.year;

  cout << "My favorite one is:\n ";
  printmyMv (mine);
  cout << "And yours is:\n ";
  printmyMv (yours);
  return 0;
}

void printmyMv (myMvs_t myMv)
{
  cout << myMv.title;
  cout << " (" << myMv.year << ")\n";
}

The example shows how we can use the members of an object as regular variables. For example, the member yours.year is a valid variable of type int, and mine.title is a valid variable of type string.

The objects mine and yours can also be treated as valid variables of type myMvs_t, for example we have passed them to the function printmyMv as we would have done with regular variables. Therefore, one of the most important advantages of data structures is that we can either refer to their members individually or to the entire structure as a block with only one identifier.

Data structures are a feature that can be used to represent databases, especially if we consider the possibility of building arrays of them:
Code:
// array of structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

#define N_myMvS 3

struct myMvs_t {
  string title;
  int year;
} films [N_myMvS];

void printmyMv (myMvs_t myMv);

int main ()
{
  string mystr;
  int n;

  for (n=0; n<N_myMvS; n++)
  {
    cout << "Enter title: ";
    getline (cin,films[n].title);
    cout << "Enter year: ";
    getline (cin,mystr);
    stringstream(mystr) >> films[n].year;
  }

  cout << "\nYou have entered these myMvs:\n";
  for (n=0; n<N_myMvS; n++)
    printmyMv (films[n]);
  return 0;
}

void printmyMv (myMvs_t myMv)
{
  cout << myMv.title;
  cout << " (" << myMv.year << ")\n";
}

Submit "Data Structures" to Digg Submit "Data Structures" to del.icio.us Submit "Data Structures" to StumbleUpon Submit "Data Structures" to Google

Tags: 1'", 1axa7a? Add / Edit Tags
Categories
Uncategorized

Comments

  1. Sanvi's Avatar
    Here is the Pointers to structures concept.

    Like any other type, structures can be pointed by its own type of pointers:
    Code:
    struct myMvs_t {
      string title;
      int year;
    };
    
    myMvs_t amyMv;
    myMvs_t * pmyMv;

    Here amyMv is an object of structure type myMvs_t, and pmyMv is a pointer to point to objects of structure type myMvs_t. So, the following code would also be valid:
    Code:
     pmyMv = &amyMv;

    The value of the pointer pmyMv would be assigned to a reference to the object amyMv (its memory address).

    We will now go with another example that includes pointers, which will serve to introduce a new operator: the arrow operator (->):
    Example for pointers to structures
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    struct myMvs_t {
      string title;
      int year;
    };
    
    int main ()
    {
      string mystr;
    
      myMvs_t amyMv;
      myMvs_t * pmyMv;
      pmyMv = &amyMv;
    
      cout << "Enter title: ";
      getline (cin, pmyMv->title);
      cout << "Enter year: ";
      getline (cin, mystr);
      (stringstream) mystr >> pmyMv->year;
    
      cout << "\nYou have entered:\n";
      cout << pmyMv->title;
      cout << " (" << pmyMv->year << ")\n";
    
      return 0;
    }

    The previous code includes an important introduction: the arrow operator (->). This is a dereference operator that is used exclusively with pointers to objects with members. This operator serves to access a member of an object to which we have a reference. In the example we used:
    Code:
    pmyMv->title
    Which is for all purposes equivalent to:
    Code:
    (*pmyMv).title

    Both expressions pmyMv->title and (*pmyMv).title are valid and both mean that we are evaluating the member title of the data structure pointed by a pointer called pmyMv. It must be clearly differentiated from:
    Code:
    *pmyMv.title
    which is equivalent to:
    Code:
    *(pmyMv.title)
    Nesting structures

    Structures can also be nested so that a valid element of a structure can also be in its turn another structure.
    Code:
    struct myMvs_t {
      string title;
      int year;
    };
    
    struct friends_t {
      string name;
      string email;
      myMvs_t favorite_myMv;
      } var1, var2;
    
    friends_t * fri = &var1;

    After the previous declaration we could use any of the following expressions:
    Code:
    var1.name
    var2.favorite_myMv.title
    var1.favorite_myMv.year
    fri->favorite_myMv.year