Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STL, Convert an element of vector<int> to a string...

Status
Not open for further replies.

mbaranski

Programmer
Nov 3, 2000
421
US
I've got a function,

/*
@ ---------------------------------------------
@ string generateCreateTableStatement()
@ called by table.gener...() and it will
@ return something like CREATE TABLE
@ IF NOT EXISTS badge (name=varchar(40) ...)
@ This will allow a table to be created easily
@ ---------------------------------------------
*/
string importTable::generateCreateTableStatement(string db_name){
string statement;

statement += "CREATE TABLE IF NOT EXISTS ";
statement += db_name;
statement += ".";
statement += name;
statement += " ( ";

int i = columns.size() - 1;
while(i > 0){
statement += columns;
statement += " ";
statement += type;
statement += "(";

statement += ((char *) (length[i--]));

statement += ")";
if(i > 0){
statement += ", ";
}
}

statement += " ) ";
cout << endl << length[1] << endl;
return (statement);
}

the line
statement += ((char *) (length[i--]));
gives a seg. fault.

length is of type vector<int>
columns and type are both vector<string>

Anyone know how to make this work, and why it's not working? All help is appreciated.

MWB As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Forget it, I fixed it, lemme know if anyone wants me to post it, here or email

mike@NOSPAM.secmgmt.com As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Hi mbaranski!

Yes, post it please!

Greetings

frag patrick.metz@epost.de
 
OK, here's the ugly workaround...

use sprintf to print the character into an old-style c-string, which happens to be a character array...

then, it's easy...

Example:

char temporary_character[48] = {'\0'};

sprintf(temporary_character, &quot;%d&quot;, length);
statement += temporary_character;

where length is of type vector<int> and statement is an stl string. This is not very elegant, but I thought it was pretty clever!

Good Luck, and let me know if it's not clear...

MWB.
As always, I hope that helped!

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top