vector not working

Started by
7 comments, last by dyerseve 18 years, 6 months ago
here's the code with the problem

        char *t;
	int size = datalength[0];
	datalength.erase(0);
	t= new char[size];
	for (int i=0;i<size;i++) 
	{
		t=data[0];
		data.erase(0);
	}
	return t;
the code works fine except for this part. datalength holds the size of the string and is the correct size. what i've been trying to do is load each character of the vector into the string while deleting the vector, and no i cannot just return the vector. can anyone point out anything wrong?
Advertisement
Is it really necessary that you delete the vector element after each copy? You could always copy each value and then just clear() the vector at the end.

and for this:

t=data[0];


Shouldnt that be t = data?

Id be interested in seeing a bit more of how your code is structured, because im sure there must be a better way than to have to write code to perform that kind of function
"Leave it to the computer programmers to shorten the "Year 2000 Millennium Bug" to "Y2K." Isn't that what caused this problem in the first place?"
this is all the code:

std::vector&lt;int&gt; datalength;std::vector&lt;char&gt; data;int dat(const char *t){	if (strlen(t)==0) {return 0;}	int size = strlen(t);	for (int i=0;i&lt;size;i++) {data.push_back(t);}	datalength.push_back(size);	return datalength[0];}char* readdata(void){	if (datalength.size()==0)	{		return "";	}	char *t;	int size = datalength[0];	datalength.erase(0);	t= new char[size];	for (int i=0;i&lt;size;i++) 	{		t=data[0];		data.erase(0);	}	return t;}


the objective of it is to add a string to data with dat() and store the size of the string in datalength, then return the first string in data with readdata() and delete the space the string occupied.
hmmm, why not just use std::string? And if you need to store lots of them just declare a single Vector<std::string>

Then if you need the length of the string, just use its length() function to return it. If you need to pass a char* to other functions, you can just call its c_str() function (which returns the same string as a char*)

Unless theres something im missing, i really cant see any reason for needing those kind of functions....
"Leave it to the computer programmers to shorten the "Year 2000 Millennium Bug" to "Y2K." Isn't that what caused this problem in the first place?"
Here's a few ways to do what you want. Note that I took out datalength in both versions:
#include <iostream>#include <vector>#include <string>std::vector< std::string > data;int dat( const char * t ) {   // no need for datalength	std::string s( t );	if( s.size() == 0 ) return 0;        data.push_back( s );	return s.size();}char * const readdata() {     // mixing C and C++	if( data.size() == 0 ) return 0;    // return null if we have no strings		char * c_string = new char[ data.front().size() ];          // destroy this later somehow ... :(	copy( data.front().begin(), data.front().end(), c_string ); // copy string to buffer        c_string[ data.front().size() ] = 0;                        // append null char	data.erase( data.begin() );                                 // erase first string	return c_string;}std::string readdata2() {     // the C++ way	if( data.size() == 0 ) return "";   // no dealing with null pointers here		std::string s( data.front() );      // copy the first string	data.erase( data.begin() );         // erase the first string	return s;}int main() {   // add some strings including null string   std::cout << "size: " << dat( "Hello, vector!" ) << std::endl;   std::cout << "size: " << dat( "Hello, string!" ) << std::endl;   std::cout << "size: " << dat( "Hello, STL!" ) << std::endl;   std::cout << "size: " << dat( "" ) << std::endl;   // read some strings using both methods   std::cout << "string: \"" << readdata() << "\"" << std::endl;  // <- leaking all kinds   std::cout << "string: \"" << readdata() << "\"" << std::endl;  // <- of memory   std::cout << "string: \"" << readdata2() << "\"" << std::endl;   std::cout << "string: \"" << readdata2() << "\"" << std::endl; // <- readdata() would crash here   return 0;}
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Your whitespace amuses me.
It's not mine. For some reason Code::Blocks is very inconsistent with tabs/spaces. It's something I deal with though. For the record, I tab @ 3, and (try to) convert those to spaces.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Hello,

Quote:Original post by flounder
here's the code with the problem
(** snip code **)
the code works fine except for this part. datalength holds the size of the string and is the correct size. what i've been trying to do is load each character of the vector into the string while deleting the vector, and no i cannot just return the vector. can anyone point out anything wrong?


You can't use the erase() method like this. The argument of erase() is an iterator, not an indexed position - in your case, it should be datalength.begin(). As Sava, I also suggest you to try to limit the use of erase(). Stylin's C++ version is quite good :)

Regards,
data.erase(0) is plain wrong. You probably want data.erase(data.begin())

This topic is closed to new replies.

Advertisement