Thread: My array question

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    to add one record to the vector you can use
    Code:
    	struct Record
    	{
    		int index;
    		std::string boy;
    		std::string girl;
    	};
    
    	Record temp ={1,"Henry", "Wilma"};
    
    	std::vector<Record> list;
    	list.push_back(temp);
    
    	std::cout << list[0].index << " " << list[0].boy << " " << list[0].girl << std::endl;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  2. #17
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    okay, so then after that i would need to have my

    Code:
    std::ifstream names_txt( "babynames2004.txt" );
    right?

    So would i use a loop to go through the vector or is there a different way since its a vector?

  3. #18
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Is there a way to not use a vector and use an array? Im way to new to programming and i really dont understand vectors.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Is there a way to not use a vector and use an array?
    yes there is
    Im way to new to programming and i really dont understand vectors.
    that's why you should go with vector - there is a lot more ways to screw up with arrays, vector does all memory menagement for you
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #20
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Well basically i have a list with rank boy name and girl name.
    example line would be like
    1 Jacob Emily

    i want the user to enter a name, and it would display like

    name is rank 1 in boys
    name is not ranked in girls

    something like that, i couldn't even begin to work with a vector cause i have zero knowledge, i have been reading about them while posting here trying to understand, but im not sure if i even could, thats why i suggested arrays since ive have written a few basic programs with them. If i were to use vectors i would really need step by step advice. Which would be great if you are willing too

    If not i understand, and ill try to screw around with it using arrays and probably ask for advice about that here as well

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    I have shown a sample how you add value to vector
    to access element - there is no differens from array

    Code:
    	std::vector<int> arr;
    	arr.push_back(1);
    	arr.push_back(2);
    	arr.push_back(3);
    
    	for(std::vector<int>::size_type i = 0; i < arr.size(); i++)
    	{
    		std::cout << arr[i] << std::endl;
    	}
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  7. #22
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Can you explain that line by line, im realyl having trouble understanding what is going on with what you are posting.

  8. #23
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Code:
    std::vector<int> arr; //declare a vector of ints
    	arr.push_back(1); // add 1 to the vector's end
    	arr.push_back(2); // add 2 to the vector's end
    	arr.push_back(3); // add 3 to the vector's end
    
    	std::vector<int>::size_type i; // declare the index var to be of type returned by size() function
    
    	//size() function shows number of elements contained in the vector
    	for(i = 0; i < arr.size(); i++) // loop through all vector's elements
    	{
    		std::cout << arr[i] << std::endl; // print current element
    	}
    
    	//same loop could be done using iterators - search forum for samples
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  9. #24
    Registered User
    Join Date
    Mar 2008
    Posts
    28
    Thanks for all you help vart, i got it all finished, using ideas you gave me on how to structure it. I didnt use vectors though. I thank you for your time and patience

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM