Archive

Archive for August, 2009

Converting a String to a Vector

August 24th, 2009 admin 1 comment

It has been a long while since I’ve posted a C or C++ tutorial, but here comes another one! I try to help with programming tips on various forums across the internet. I am a fluent English and Italian speaker, so naturally I work on forums of both languages. While browsing an Italian forum, I came across an interesting question. How does one convert a C++ string to a vector. I decided I would lend a hand, and it bears repeating on here.

Vectors can be used for various things in C++, but they are for the more advanced programmer really. They are not really necessary if it is not a complex program, but this tutorial could serve useful for many I’m sure.

I myself was at first puzzled by the question. I had never thought of a reason to do this and so, frankly, I never have. After some thought, it wasn’t too bad, but still an interesting concept. I’ll post the code below and then explain further below that. Comments are in both English and Italian. The reason being is what I previously mentioned about the original reason I wrote this code.

/**
* String to Vector Tutorial by Dennis M.
*
* un tutorial di microsonic.org
*
*/

// Include files ~ Includere i file
#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
// Declare Variables ~ Definire i varibili
string data = "one - uno";
vector<string> vect;

// Insert data into vector ~ Inserire l'informazione in il vector
vect.push_back(data);
data = "two - due";
vect.push_back(data);
data = "three - tre";
vect.push_back(data);

// Loop to view the contents of the vector ~ Loop per vedere i contenti di il vector
for(unsigned int i=0;i<vect.size();i++){
cout<< i << ": " << vect.at(i) << endl;
}

// Memory Management ~ Ci sicuriamo la memoria!
vect.clear();

return 0;
}

Now the code is pretty self-explanatory and the comments I think do a pretty good job. The only thing one may be perplexed about is where the functions and pointers come from. If you examine the documentation (header file?) for a vector, all is clearly defined. This example will also print the vector and clear it before it exits.

So I hope this post is of some service to someone and as usual, I have included the source and binaries in the post!

String to Vector Tutorial

Regards,
Dennis M.

Categories: C/C++, Italiano, Other Tags:

Importance of Structure and Coding Etiquette

August 7th, 2009 admin No comments

Well, it’s been a very long time since I last updated and I’d like to apologize to all my subscribers for that. I’ve been very busy, but it seems the work load is going down and I’ll have more time to continue writing! Now, on with the article.

So recently, I have just finished a project where one developer had started and then decided he could not finish the work, so I was hired to finish it. The natural thought to one who is inexperienced is, “This will be a cakewalk. Most of the programming is already done!” – wrong. The first thing that went through my mind was, “I wonder how bad this really is.” So, I accept the project (as I had only a few projects at the time) and take a look.

The code was atrocious to say the least. I felt as if this other developer had never learned how to use comments or his tab key/space bar to format. Most of the time on the project was bent around figuring out what the original developer had tried to do. It was a nightmare.

As I started digging through files and files of unnecessary sloppy code, I thought to myself, “I need to write something about this. This kind of work needs to stop.” I was not upset because of the amateur programming, nor the fact that it was undocumented and poorly written. What bugs me is the fact that someone paid for that kind of work. It looked like the developer copy/pasted everything from snippets he or she found online. That being said, one must learn the importance of structure and coding etiquette.

Structure is important for general organization. It keeps code neat and clean looking and much easier for anyone, to include yourself, to go back and fix errors/security holes. Most people see structural formatting as a simple aesthetic quality when in reality it is like formatting a letter. The structure keeps things organized and understandable on a more universal level.

Coding etiquette, on the other hand, is something learned over a long period of time. No new developer can simply logon and expect to program to the standards set right now, but at the same time should begin mimicking the styles of major developers. Examining the work of others is one of the best ways for any developer to learn, so studying (yes, just like in school) the work of past developers, and prominent works of today, one can easily understand how to program professionally. A simple example would be to write functions rather than hardcode functions multiple times. Or rather than using raw MySQL functions, create an SQL wrapper to execute the functions.

There are many resources on learning how to program professionally, and be neat, but it’s up to developers to use the tools. The vast majority of developers, I would say, hold to the standards. However, for those who do not, they are just ripping off their client in the long-run.

Regards,
Dennis M.