Thread: Vectors in Arrays

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    Vectors in Arrays

    Is there any way to pass a vector in the creation in an array besides creating a vector outside of the array and then passing the variable? Sorta like...

    Code:
    std::vector<std::string> array[] = {
        <create a vector here>,
        <create a vector here>,
        <create a vector here>,
        <etc...>
    };
    Is anything like that possible, or am I gonna have to do it the hard way?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is anything like that possible, or am I gonna have to do it the hard way?
    You'll probably have to do it the hard way unless you want to use Boost to initialize the vector or one of the standard constructors does what you want. For example:
    Code:
    #include <cstddef>
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main()
    {
    	std::string init[] = {
    		"This","is","a","test"
    	};
    	std::vector<std::string> array[] = {
    		std::vector<std::string> ( 5, "test" ),
    		std::vector<std::string> ( init, init + 4 )
    	};
    
    	for ( int i = 0; i < 2; i++ ) {
    		for ( std::size_t j = 0; j < array[i].size(); j++ )
    			std::cout<< array[i][j] <<' ';
    		std::cout<<'\n';
    	}
    }
    [edit]
    Oh. my. god. Does this board really use a tab size of 8?
    [/edit]
    My best code is written with the delete key.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Prelude View Post
    [edit]
    Oh. my. god. Does this board really use a tab size of 8?
    [/edit]
    Yep. First time?

    On another note, you can do a vector of vectors:
    Code:
    std::vector< std::vector<std::string> > v;
    Or you can use boost's multi-array, though I dunno how it works.
    There's no need to do things the hard way...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Argh, forget it then, I'll just stick to an array instead of a vector. x_x

    On another note, how would one go about determining if an element in an array of strings was empty?

    Code:
    void TurnerBot_Util::copy(char* array[], vString &v) {
        for(int i = 0; i < TURNERBOT_MAX_RESP; ++i) {
            if(array[i] != NULL) {
                v.push_back(array[i]);
            } else {
                break;
            }
        }
    }
    That's an old function I have, but I need to make it work for a std::string element. This was my feeble attempt at it, but it doesn't compile. Tells me that there's no operator to test for equality in a string?

    Code:
    void TurnerBot_Util::copy(std::string array[], vString &v) {
        for(int i = 0; i < TURNERBOT_MAX_RESP; ++i) {
            if(array[i] != NULL) {
                v.push_back(array[i]);
            } else {
                break;
            }
        }
    }
    Last edited by Osiris990; 02-25-2008 at 02:43 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unfortunately, vector lacks initializers. But C++0x should fix that. Until then, you can try to make your own or live with it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how would one go about determining if an element in an array of strings was empty?
    Code:
    if ( s.empty() ) {
      // I'm an empty string
    }
    else {
      // I'm not empty!
    }
    >Tells me that there's no operator to test for equality in a string?
    Not against an integer, which NULL is in C++:
    Code:
    #define NULL 0
    >Yep. First time?
    It's especially shocking as I prefer a tab stop of 2...
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays vs Vectors
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 02:06 AM
  2. vectors vs c style arrays
    By markucd in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2006, 11:11 AM
  3. byte arrays & vectors
    By kasun in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 09:10 AM
  4. arrays or vectors
    By Geo-Fry in forum C++ Programming
    Replies: 26
    Last Post: 04-17-2003, 07:08 PM
  5. arrays and vectors
    By volk in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2003, 03:45 PM