Appending an unsized array

This is a discussion on Appending an unsized array within the C++ Programming forums, part of the General Programming Boards category; Is there a way to append an unsized array (of strings) to the end of another one? I have the ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    Appending an unsized array

    Is there a way to append an unsized array (of strings) to the end of another one? I have the following incomplete function:

    Code:
    String* all_files(String* sDir)[]
    {
    		String* f[] = Directory::GetFiles(sDir);
    		int numFiles = f->get_Length();
    
    		// The new items in f[] are added to the old ones and stored in files[] (the problem)
    
    		String* d[] = Directory::GetDirectories(sDir);
    		int numDirs = d->get_Length();
    
    		for (int i=0; i < numDirs; i++)
    		{
    			all_files(d[i]);
    		}
    
    		return files;
    }
    (try-catch omitted).

    The function basically is *supposed* to return an array of strings containing the full path of all the files in sDir. But I can't figure out how to add the new items in f[] to the old ones to generate the output. Btw as you can see I don't have any definitions for variable "files"; this is because I don't know where to define it. Vectors didn't work here.

    Any help is appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Is there a way to append an unsized array
    All arrays are required to have sizes, so there is no such thing as an "unsized array".
    Vectors didn't work here.
    Why not?
    Last edited by 7stud; 01-22-2007 at 11:45 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    Maybe I wasn't using the right syntax for vectors, but I tried

    Code:
    vector<String*> f = Directory::GetFiles(sDir);
    and it didn't work. I also tried vector<String> (without *) and still same thing... Like I said I'm not sure if I used the proper syntax, but I couldn't think of any other ways.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1) There's no such type as String or STring or strinG or striNG or strIng or sTrInG.

    2) The simplest way to add elements to a vector is by using a vector's push_back() method. There is also a copy <algorithm> that you might find useful, e.g.

    Code:
    #include<iostream>
    #include<string>
    #include<vector> 
    #include<algorithm> //copy(), back_inserter
    
    using namespace std;
    
    int main()
    {
    	vector<string> v;
    	v.push_back("hello");
    	v.push_back("world");
    
    	string arr[] = {"hello", "moon"};
    	int size = 2;
    	
    	vector<string> v2;
    	v2.push_back("goodbye");
    	v2.push_back("world");
    
    	copy(&arr[0], &arr[size], back_inserter(v)); 
    	//Specifies the range, which includes first but excludes last, i.e.
    	//the elements from start to end - 1 are the elements in the range.
    	//Back_inserter inserts the range at the back of v.
    
    	copy(v2.begin(), v2.end(), back_inserter(v));
    
    	vector<string>::iterator iter = v.begin(); //'iter' is like a pointer to v.begin();
    	while(iter != v.end())
    	{
    		cout<<*iter<<" ";
    		++iter;
    	}
    	cout<<"."<<endl;
    
    	return 0;
    }
    3) If you can legally write this:

    String* f[] = Directory::GetFiles(sDir);

    then you can't write this:

    vector<String*> f = Directory::GetFiles(sDir);

    What type is f in the first line? What type is f in the second line?

    More importantly what type does Directory::GetFiles(sDir) return? Whatever type it returns is the type of the variable you must declare in order to store the function's return value in that variable.

    You need to read a tutorial on vectors. Just guessing what function's are available, e.g opertator=, is not usually going to work in C++.
    Last edited by 7stud; 01-22-2007 at 11:56 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    String is in mscorlib.dll and is a part of the .NET framework.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You know this is a C++ forum, right?

  7. #7
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    I am writing my code in a C++ project, in a .cpp file, and all I have done is that I included mscorlib into my code. Since this dll is being used almost widely in C++ projects I guessed that someone would know s/th about it in this forum. But what matters is that the question I asked was for a C++ code; I was wondering if there's a simple method to append an array with another one in C++; the code was merely to provide better understanding...

  8. #8
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    Alrite, finally I managed to solve this...

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Questions about .NET framework classes fit better in the C# forum really. unsized arrays are not in standard C++

  10. #10
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    Quote Originally Posted by KIBO
    Questions about .NET framework classes fit better in the C# forum really. unsized arrays are not in standard C++
    I posted the exact same question in Microsoft's C# forum, and shortly it was moved to the C++ forum by the mods... and by unsized array I meant an uninitialized array (maybe I used wrong wording). Anyway this is done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  2. Unsized array problem
    By qwertysingh in forum C Programming
    Replies: 4
    Last Post: 03-12-2009, 01:17 PM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. appending to an array
    By mr_nice! in forum C Programming
    Replies: 1
    Last Post: 06-28-2004, 10:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21