-
February 16th, 2004, 05:19 PM
#1
Is this use of new and delete correct ?
PHP Code:
string ** p;
p=new string*[2];
delete [] p;
Please answer me...Okay ?
Thanks a lot
-
February 16th, 2004, 05:21 PM
#2
Yes, but why would you want to allocate pointers to string objects?
All the buzzt
CornedBee
-
February 16th, 2004, 05:21 PM
#3
-
February 16th, 2004, 05:38 PM
#4
Thanks gg(codeplug) and CornedBee for your answers...
Originally posted by CornedBee
Yes, but why would you want to allocate pointers to string objects?
I would like to use 2d array (in my program now, I am using vector of vector) to find the longest string input from user...
I am still geting stuck at how to pass my vector-of-vector (2d array) to the function that usually works fine if my argument is something declared as array[10][10], I used vector<vector>array, so now my VC++ compiler tells me that it cannot convert the first parameter of function output(array, count) from (vector<vector<string>,allocate<>.......) into string.
I dont know why it happens and what I should do to fix it ?
Do you know what might be the problem ? How I can pass my parameter more correctly ?
Thanks a lot,
-
February 16th, 2004, 06:46 PM
#5
If you are using a "vector of vectors", passing it to a function should be trivial. (It's no different from passing an int for instance)
Code:
#include <vector>
#include <string>
void Func(const std::vector<std::vector<std::string> >& p_crVectorVector)
{
// Print or whatever here...
}
int main()
{
std::vector<std::vector<std::string> > SomeVector;
// Manipulate SomeVector here...
Func(SomeVector);
return 0;
}
-
February 16th, 2004, 06:54 PM
#6
Originally posted by Assmaster
If you are using a "vector of vectors", passing it to a function should be trivial. (It's no different from passing an int for instance)
Code:
#include <vector>
#include <string>
void Func(const std::vector<std::vector<std::string> >& p_crVectorVector)
{
// Print or whatever here...
}
int main()
{
std::vector<std::vector<std::string> > SomeVector;
// Manipulate SomeVector here...
Func(SomeVector);
return 0;
}
Thanks a lot,
But the problme is that I copied tha code from the net and it is used for output(array[10][10], n) while i implemented my array as vector of vector, I dont think I can change the whole code into STL because I am bad at programming, and even when I use it, I still dont understand what it content is ? I just know it will output my array and that s all..
<edit>I typed in a hurry and made so many typos</edit>
Last edited by Charleston; February 16th, 2004 at 07:01 PM.
-
February 16th, 2004, 07:09 PM
#7
If your output function takes a 2D array as a parameter, you can't easily pass your vector of vectors to it without doing some conversion.
But if all you need is to print the contents of your vector, I'm in a giving mood today:
Code:
#pragma warning(disable: 4786)
#include <iostream>
#include <vector>
#include <string>
void Print(const std::vector<std::vector<std::string> >& p_crVectorVector)
{
for(std::vector<std::vector<std::string> >::const_iterator i = p_crVectorVector.begin(); i != p_crVectorVector.end(); i++)
{
for(std::vector<std::string>::const_iterator j = (*i).begin(); j != (*i).end(); j++)
{
std::cout << (*j) << " ";
}
std::cout << std::endl;
}
}
int main()
{
std::vector<std::vector<std::string> > SomeVector;
// Fill the vector with some test data:
SomeVector.resize(2);
SomeVector[0].resize(2);
SomeVector[0][0] = "Some text";
SomeVector[0][1] = "Some more text";
SomeVector[1].resize(1);
SomeVector[1][0] = "Even more text";
// Print it:
Print(SomeVector);
return 0;
}
You should easily be able to customize the Print function to suit your needs, as to formatting and whatnot.
-
February 16th, 2004, 07:17 PM
#8
Okay, I am also in a saying-thanks-to-you mood today
Seriously, Thanks so much,
<edit>I forgot a word "mood" and many a dash</edit>
Last edited by Charleston; February 16th, 2004 at 07:22 PM.
-
February 17th, 2004, 09:06 AM
#9
May I ask another question ?
What is #pragma warning(disable: 4786) ???
Why do you have to use it ?
Thanks again
-
February 17th, 2004, 09:23 AM
#10
It's there just to disable an irritating warning in VC++ 6.0 that occurs when using some of the STL containters. (Particularily std::map) It might not be needed in this example, but I usually include this line anyway since the warnings may seem somewhat cryptic to people that don't know the reason for them.
If you are using another compiler, you should probably remove this line, though I'm not sure.
Last edited by Assmaster; February 17th, 2004 at 09:27 AM.
-
February 17th, 2004, 09:27 AM
#11
Originally posted by Assmaster
It's there just to disable an irritating warning in VC++ 6.0 that occurs when using some of the STL containters. (Particularily std::map) It might not be needed in this example, but I usually include this line anyway since the warnings may seem somewhat cryptic to people that don't know the reason for them.
Thanks again,
-
February 17th, 2004, 09:29 AM
#12
This article explains this topic very well.
-
February 17th, 2004, 09:46 AM
#13
Originally posted by bluesource
This article explains this topic very well.
Thanks for that nice link.
I am using DevC++, VC6++, WatCom, MingW(GNU) for windows,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|