STL vector and Visual C++ 6.0

Hey. Once again I’m asking the experienced coders of SD to point me in the right direction. I’m working on a rather involved project in an algorithms class and I need a dynamic array to hold my input data, several thousand 32-bit unsigned integers. I’m trying to use a vector, but I’m not using a format for declaring it that Visual c++ seems to agree with.

Below is a code snippet. This seems to agree with the Standard STL documentation on the web (like this google result ). Except I can’t figure out what I’m supposed to pass for the alloc argument. All of the sample code I’ve seen doesn’t use this second argument.


//global, unsorted data
vector <unsigned int> Unsorted;

//a function that will read 32-bit unsigned ints from a file
vector <unsigned int> GetData(char* path);

The relevant errors returned by VC++ on compilation are:
(24) : error C2143: syntax error : missing ‘;’ before ‘<’
(24) : error C2501: ‘vector’ : missing storage-class or type specifiers
(24) : error C2086: ‘vector’ : redefinition

Thanks for your help. I’ve spent the last 3 years in college learning theory and concept. I’ve lost most of my skills for interpreting language documentation. Even the clearly worded versions that VC++ provides, HA HA HA!

Oh, if it matters, for this project the code is not graded, it just has to work. The grade is based on my analysis and reporting of the results. We were told outright that we could beg/borrow/buy code from last semesters students so long as we did our own analysis. I don’t trust others work, so I’m rolling my own.

Did you

#include <vector>

and put in the using statement

using namepace std;
?

Yes on the include, I just tried the using namespace std; part. It seemed to have solved the problem. Now I’m getting a few undeclared errors from spelling mistakes.

What does ‘using namespace std;’ do? Tell the compiler to use the standard libraries? I thought that would be handled with an include statement.

This compiles for me:


#include <vector>
std::vector <unsigned int> theVec;
std::vector <unsigned int> GetData(char* path)
{
	return theVec;
}

int main()
{
	return 0;
}

It tells the compiler that you want classes and stuff in the namespace ‘std’ to be available without fully qualifying them.

In other words, instead of ‘using namespace std;’ you should have also been able to declare your vectors as
std::vector <unsigned int> Unsorted;

MSDN has more info on namespaces - if you don’t use them, and you’re involved in large projects, they’re a great organizational tool. Plus, if you learn them, you’ll have a leg up on learning C#, in which everything belongs to a namespace.

Yes, basically it tells it which namespace to look for templates and such under. Alternatively you can always explicitly name the object’s namespace or class (same syntax) such as:
std::vector <unsigned int> theVec;.

When you put a using statement in, you’re setting the scope of the declarations. For example, you might overload vector in your own class (why, I don’t know, but you can). Here you’d refer to the CFoo class’s vector or the standard vector like:
CFoo::vector
std::vector.

Perhaps you’re using the namespace standard, in which case you’d use
CFoo::vector
vector (implied std::).

You might not want to return an entire vector, though.


#include <vector>
std::vector <unsigned int> theVec;
std::vector <unsigned int> *GetData(char* path)
{
	return &theVec;
}

int main()
{
	return 0;
};

I don’t know offhand how the memory handling works for returning vectors or what a vector is (maybe it is just a reference to the first element?), but if you can avoid tossing entire objects around I say do it.

No, a vector is a class is the truest OO sense - the internal implementation is truly separated from the clients’ interactions. You can’t trust AT ALL where the memory within a vector might be, or that it’s contiguous. It does support the operator, but it’s not an array offset.

Nor are iterators through STL classes indexes, nor pointers. They’re just iterators you’re not allowed to ask about how they work.

Nevertheless, of course, you can pass them by reference, as anything in C++ can be, and improve efficiency.

So what would that say about returning a vector? Are you returning all the pointers to all the elements? Or are you suggesting that we’re not allowed to know what happens when we return a vector (I don’t think that’s right)?

I know some programming but not all the memory info. So if theVec is a vector, and we pass by value, it makes a copy of all the elements (I seem to recall writing a program that demonstrated just this fact). But if we return a vector… what then?

Our op only says he’s dealing with a few megs of memory in this case, so it probably isn’t the biggest concern ever, but I do wonder.

No, just like with any pointer, you’re passing to the function, and getting back, the memory address at which the instance of the class resides. You’re passing 4 bytes - a long int.

What I’m saying is you can’t treat that thing like an array - you can still only deal with it through the interface available. Specifically, you asked a couple of posts up if it was the address of the first element - it’s not.

I see. Thanks.