Vectors and Structures in C++


16 Mar  

In this edition of techblog we will discuss about the concept of vectors and structures in C++. Please note that in order to fully comprehend the ideas presented here, you need to have a basic understanding of C++ syntax (If not, please go to the ‘C/C++’category and read the previous entries).

Vectors

Vectors are used to store data collections. Some of you might be wondering why we need vector when we already have arrays. Well, let me clarify this point. Though array can also be used for storing the information, its size is fixed. This means that you can’t ‘change’ the size of the array (number of elements) dynamically. I also want to clarify the fact that you can have the number of elements of an array as ‘n’ in a program, and then get a value for n from the user. But, this size cannot be modified further. In case with vector, we can do that.

In this edition, I will show the basic operations using vectors. And we will be using the dynamic nature when we deal with ANN programs. You can represent a vector as:

vector<data_type> var_name (number);

 

As I said before, the size of the vector is dynamic. Hence it is valid to define a vector as

vector<data_type> var_name;

 

One thing that you need to note is that you have to include the ‘vector header file’ while meddling with vectors in C++. Let me illustrate the concept with a sample code:

 

#include <iostream>
#include <vector> 

using namespace std; 

int main ()
{
    vector<int> nos;
    int n;
    int myno;
    int display_no; 

    cout <<"Enter the total number of elements?" << endl;
    cin >> n;
    nos.resize(n); 

    for (int i=0;i<n;i++) { 

      cout << "Enter digit no " << (i+1) << endl;
      cin  >> myno;
      nos[i]=myno;
    } 

    for (int j=0;j<n;j++) { 

        cout << "Entry " << (j+1) << "is" << nos[j]  <<endl; 

    } 

    return 0;
}

 

Here you can see that the number of elements are assigned by the user. Then, we use the resize() function (from vector header) to set the size of the vector. The rest of the code is self explanatory.

 

vector in c++

 

If you want to see the dynamic nature of vector size, see this code:

 

#include <iostream>
#include <vector>

using namespace std;

int main ()
{
    vector<int> nos;
    int n;
    int n1;

    cout <<"Enter the total number of elements?" << endl;
    cin >> n;
    nos.resize(n);
    cout <<"No of elements= " << nos.size() << endl;
    n1=nos.size();
    n1++;
    nos.resize(n1);
    cout <<"Incrementing the size of vector"<< endl;
    cout <<"No of elements= " << nos.size()<< endl;

    return 0;
}

 

And here is the demo:

 

changing size of vector

 

Structures 

 

Structures are also similar entities. But the difference is that it can hold different types of data together (say, char and int). Let me show the code now:

 

#include <iostream> 

using namespace std; 

int main ()
{
    struct user {char name[10]; int id;}; 

    user sampleuser={"user1",1};
    cout << sampleuser.name<<endl;
    cout << sampleuser.id << endl; 

    return 0;
}

And here is the code in action:

 

struct in c++

 

Structures are largely used in programs. As you can see in the code, they allow you to store different data types together as a set.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Tags: , ,


TechBlog on Facebook

Leave a Reply