Thursday, October 14, 2010

C++ Snippets on Linux: Vectors Vice Arrays As a Better Way to Store Data

You may have read and enjoyed my recent article "C++ Snippets: Converting Hexidecimal Values to Decimal Values." In that article, I briefly discussed a secret project that I have undertaken that will eventually result in my first GUI application for GNU/Linux, Windows, and perhaps even MacOS. At that time, I said that I could not reveal the exact nature of the program. I still cannot reveal the exact nature of the program, but I am releasing more of the source code under the GNU GPL license version 3. If you look at this code, run and compile it, you may glean a few more hints as to what kind of program I am actually aiming to write. in this article, I will reveal a few more details as to how I came up with this program idea.

I suspect that programmers tend to write programs with two considerations in mind:

1. What software do I need to do the things that I want to do?
2. What software do others need to do the things that they want to do?
Vectors Versus Arrays
Question number one was the one that inspired me to start off on this software project. I had something that I wanted to do on my GNU/Linux laptop, but I could not find free software that would allow me to do it. I did find plenty of proprietary software that would do the job, but at a cost of several hundred dollars that I do NOT want to spend. So I started doing the research necessary to create my own software to do the job. Immediately, I began to realize that writing software is fundamentally a task of solving a series of technical challenges. My first challenge was converting hexadecimal data to decimal format. That problem was solved by the 1st bit of source code that I released. The next technical challenge was that of storing dynamic data with a text file. This is where I came to the important decision point of storing data is arrays or vectors. Arrays and vectors are very similar constructs, both store data in a series of sequential memory locations. However, vectors are much more powerful and much more flexible. Generally speaking, arrays are fixed in size at the time of their creation. Vectors, on the other hand, can be dynamically resized. In fact, the size of a vector does not have to be declared when you create it, it can just grow as you add more data two it. The compiler that I use within GNU/Linux, gcc, inherent supports both types of structures. So I decided to use vectors as my storage medium. The next decision was to decide which software to use to write my program.

Bluefish Editor and Code::Blocks
GNU/Linux has two very powerful programs that I love to use for writing C++ code, Bluefish Editor and Code::Blocks. I use Fedora Linux 13, and it has both programs in its repositories. Bluefish allows you to write your code with all of the appropriate highlighting, and Code::Blocks allows you to test, compile, and run the code.

Writing the Code
Writing the code has not been that hard but it does require a lot of research because I do not have every piece of the C++ structure memorized. I use two resources as I code: a textbook and Google. The C++ textbook that I use is the very well respected "C++ How To Program Seventh Edition" by Paul and Harvey Deitel. It is very good and very thorough. However, if I can't find a good answer on how to solve a particular problem in this text, I then resort to Google searches to find the answer. For example, this textbook did not really provide a good answer on how to create multidimensional vectors. However, I was able to find a relatively simple solution through code that I found via a Google search. I did not cut and paste the code that I found, but it gave me enough of an insight into how C++ works that I was able to quickly write the code myself, You will see this below.

The Code
Last but not least, I will present the code for today's article. Feel free to use it, edit it, improve upon it, and share it to your hearts content. You can read the terms under which the code is licensed here:
The source code, a GNU/Linux binary, and documentation, are provided for download at the end of this article. Enjoy!

The source code for Vector in html format is here:

No comments:

Post a Comment