Thread: multidimentinal arrays (vectors)

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    34

    multidimentinal arrays (vectors)

    I am having trouble putting values into a 2D vector. I'm trying to put the X into one spot and then have the 2 be in the other. so it will eventually be like this:

    vector[0] = X
    vector[0][0] = 2
    vector[1] = Y
    vector[1][0] = 4

    or does it have to be like this?

    vector[0][0] = X
    vector[0][1] = 2
    vector[1][0] = Y
    vector[1][1] = 4

    anyways, here's the code if someone could help me.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	vector< vector<string> > stringVectors;
    	
    	aSecondSubString = " X=2 Y=4";
    	unsigned k = 0;
    	string newString;
    
    	while (aSecondSubString[k] != '\0')
    	{
    		//cout << "newString" << k << " is " << newString << endl;
    		if (aSecondSubString[k] != ' ' && aSecondSubString[k] != '\t' && aSecondSubString[k] != '\n')
    		{
    			if (aSecondSubString[k] == '=')
    			{
    				stringVectors[stringVectors.size()].push_back(newString);
    				newString = "";
    			}
    			else
    				newString += aSecondSubString[k];
    				
    			if (aSecondSubString[k+1] == '\0' || ((aSecondSubString[k+1] == ' ' || aSecondSubString[k+1] == '\t' || aSecondSubString[k+1] == '\n') && newString.length() > 1))
    			{
    				stringVectors[stringVectors.size()][0].push_back(newString);
    				newString = "";
    			}
    		}
    		k++;
    	}
    	
    	unsigned j;
    	for (j = 0; j < stringVectors.size(); j++)
    	{
    		//cout << j << ": " << "\"" << stringVectors[j] << "\"" << "Value: \"" << stringVectors[j][0] << "\"" << endl;
    	} 
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    vector[0] is a vector<string>. If you want it to look like this:
    Code:
    X 2
    Y 4
    then what you want is:
    Code:
    vector[0][0] = X;
    vector[0][1] = 2;
    vector[1][0] = Y;
    vector[1][1] = 4;
    >> while (aSecondSubString[k] != '\0')
    Is aSecondSubString a string? If so, you can't check for '\0', because there might not be a null character terminating that string. You should be looping from k = 0 while k < aSecondSubString.size() - 1.

    >> stringVectors[stringVectors.size()].push_back(newString);
    This is bad. stringVectors[stringVectors.size()] doesn't exist, so you shouldn't be trying to use it. You need to push_back a vector first, and then maybe access it by using back().

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    >> stringVectors[stringVectors.size()].push_back(newString);
    This is bad. stringVectors[stringVectors.size()] doesn't exist, so you shouldn't be trying to use it. You need to push_back a vector first, and then maybe access it by using back().
    So could i resize it and then try that? Or can I only assign variables in using the [0][0] syntax? Is there anyway I can assign something to the vector[0] position and the vector[0][0] positions using push_back? I am a little confused, sorry, and thank you for your reply.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If your vector is empty, then calling push_back will put something into vector[0]. If you have vector<vector<string> >, then you have to push_back a vector<string> to get a value at vector[0] if the vector was empty. Once you do that, you then have to push_back a string to get a value at vector[0][0] (because vector[0] would be an empty vector).

    There are other ways to do it. If you call resize on a vector, then it automatically adds that number of values. If you know your vector will be two by two, then you can do this:
    Code:
    vector<vector<string> > stringVectors(2, vector<string>(2));
    That will start the vectors with 2 values each, so then you never have to use push_back, you can just use vector[0][0], vector[0][1], vector[1][0] and vector[1][1].

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    34
    So, I have changed my code to this:

    Code:
    while (k < aSecondSubString.size()-1)
    	{
    		//cout << "newString" << k << " is " << newString << endl;
    		if (aSecondSubString[k] != ' ' && aSecondSubString[k] != '\t' && aSecondSubString[k] != '\n')
    		{
    			if (aSecondSubString[k] == '=')
    			{
    				stringVectors.push_back(newString);
    				newString = "";
    			}
    			else
    				newString += aSecondSubString[k];
    				
    			if (aSecondSubString[k+1] == '\0' || ((aSecondSubString[k+1] == ' ' || aSecondSubString[k+1] == '\t' || aSecondSubString[k+1] == '\n') && newString.length() > 1))
    			{
    				stringVectors.push_back(newString);
    				newString = "";
    			}
    		}
    		k++;
    	}
    If I understand you correctly, then the first stringVectors.push_back(newString); should be pushing "X", and then that will give me access to stringVectors[0], and then it will pass by the "=" and get to "2", which it then will push_back, (into spot stringVectors[0][0] ?), then i will have access to stringVectors[0][0], and then the same to stringVectors[1], and so on. However, at the moment I am getting the error below.

    Code:
    substr_multidim_vector_test.cc:47: error: no matching function for call to 'std::vector<std::vector<std::basic_string<char, std::char_traits<char>,
     std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>,
     std::allocator<char> > > >, std::allocator<std::vector<std::basic_string<char, std::char_traits<char>,
     std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>,
     std::allocator<char> > > > > >::push_back(std::string&)'
    Any ideas? I know it says no matching function call, but I can't figure out what's wrong with that line. Thanks.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    stringVectors holds vectors, not strings. So stringVectors.push_back(newString) doesn't make sense, newString is a string, not a vector of strings.

    You need to push a vector<string> on to stringVectors, then stringVectors[0] will hold something and you can push newString on to that.

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