Vector of Character Arrays
I need to make a vector of character arrays for a program I am writing but I can't figure out how to do it. I'm new to C++ - I tried doing:
vector<char*> v;
but it didn't seem to work properly. How would I go about doing this?
Thanks much
# 1 Re: Vector of Character Arrays
It should work properly (however that char* would be a weak link, so that you'll need to manage objects placed in that vector externally). But most likely it would be better to use string instrances instead on char * pointers:vector<string> v;
# 2 Re: Vector of Character Arrays
It should work properly (however that char* would be a weak link, so that you'll need to manage objects placed in that vector externally). But most likely it would be better to use string instrances instead on char * pointers:vector<string> v;
I agree that it would be better to use String, but it's a program for a class and the program specification requires me to use a vector of type character string.
How would I initialize such a vector?
# 3 Re: Vector of Character Arrays
#include <vector>
vector<char[size]> identifier;
# 4 Re: Vector of Character Arrays
#include <vector>
vector<char[size]> identifier;
I tried that, it didn't seem to work properly.
Can anyone confirm that this is correct?
When I tried to do:
vector<char[size]> identifier(num);
It gave type conversion errors, so I figured it was incorrect syntax.
# 5 Re: Vector of Character Arrays
I agree that it would be better to use String, but it's a program for a class and the program specification requires me to use a vector of type character string.
How would I initialize such a vector?First, a char* is a pointer to a character. It is *not* a character string. Yes, the char * may point to the beginning of a character buffer, but it still is only a pointer. Similar to an int* only being a pointer to int -- it is not an array of int.
Second a std::string *is* a string. It is more of a string than a "char *" is to a string. You want a string, RoboTact gave you what you asked for -- string.
Third, you initialize a pointer just like any other pointer. You assign it an address, or NULL.
Fourth, if you don't take the advice of using std::string, I can almost guarantee you will be getting all sorts of weird problems, crashes, memory overwrites, etc, when you use a vector<char *> in anything but a toy, almost do-nothing program.
Fifth, please clarify with whoever gave you the assignment what exactly do they mean by "character string". In C++, a character string is accomplished by declaring a std::string type.
Regards,
Paul McKenzie
# 6 Re: Vector of Character Arrays
I tried that, it didn't seem to work properly.
Can anyone confirm that this is correct?
When I tried to do:
vector<char[size]> identifier(num);
It gave type conversion errors, so I figured it was incorrect syntax.This is wrong. The type that vector holds must be assignable and copyable. An array is neither of these. Also, you can't initialize an array with a variable index size -- it must be constant.
Regards,
Paul McKenzie
# 7 Re: Vector of Character Arrays
If it requires vector of character strings, class string seems OK, unless it requires vector of character arrays. If arrays are of fixed size, use Frogaholic's solution, otherwise you need a wrapper managing pointer (smart pointer) or external management of destruction/copying vectors if you really need strong versions (what were the problems with initial solution?).
# 8 Re: Vector of Character Arrays
#include <vector>
vector<char[size]> identifier;No will not work. The "solution" (even though std::string *is* the right choice) is the following:
#include <vector>
struct MyString
{
char theString[100];
};
int main()
{
std::vector<MyString> identifier;
}
Since a struct is copyable and assignable, then this works. But it is far inferior than just using std::string for many reasons.
Regards,
Paul McKenzie
# 9 Re: Vector of Character Arrays
Great, thanks a lot for the help.
# 10 Re: Vector of Character Arrays
Another alternative is a vector of vector char.
vector<vector<char> > MyArrah;
Be aware, that above type will require more work to get it to work with string functions, since the NULL terminator is not automatically handle like in a std::string type.
Axter at 2007-11-9 0:52:40 >
