Sign in to follow this  
flounder

vector not working

Recommended Posts

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[i]=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?

Share this post


Link to post
Share on other sites
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[i]=data[0];


Shouldnt that be t[i] = data[i]?

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

Share this post


Link to post
Share on other sites
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[i]);}
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[i]=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.

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this