CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2004
    Posts
    138

    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

  2. #2
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    Yes, but why would you want to allocate pointers to string objects?
    All the buzzt
    CornedBee

  3. #3
    Join Date
    Nov 2003
    Posts
    1,902
    Yes.

    gg

  4. #4
    Join Date
    Feb 2004
    Posts
    138
    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,

  5. #5
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    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;
    	}

  6. #6
    Join Date
    Feb 2004
    Posts
    138
    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.

  7. #7
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    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.

  8. #8
    Join Date
    Feb 2004
    Posts
    138
    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.

  9. #9
    Join Date
    Feb 2004
    Posts
    138
    May I ask another question ?
    What is #pragma warning(disable: 4786) ???
    Why do you have to use it ?

    Thanks again

  10. #10
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    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.

  11. #11
    Join Date
    Feb 2004
    Posts
    138
    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,

  12. #12
    Join Date
    Jun 2001
    Location
    Michigan
    Posts
    2,222
    This article explains this topic very well.

  13. #13
    Join Date
    Feb 2004
    Posts
    138
    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
  •  





Click Here to Expand Forum to Full Width

Featured