Hi, I am new to C++ and I am gettting confused with arrays and vectors. What exactly is the difference?
I want to create an array/vector of strings, with 8 rows and 8 columns, and then to insert strings into the array in a for loop, but I have no idea how to add to an array once it is created, please help!
A vector is a C++-feature which handles the memory for you, like resizing the space for objects, etc. Arrays doesn't have these features, so you've to handle the memory yourself; resizing, allocation, etc. If you know a fixed size, like 8 x 8, then you can easily use an array,
This is a simple example, using arrays.
Code:#include <string> // ... std::string My8x8array[8][8]; My8x8array[0][0] = "0x0"; My8x8array[0][1] = "0x1"; // ... My8x8array[7][6] = "7x6"; My8x8array[7][7] = "7x7"; std::cout << My8x8array[0][0] << std::endl; std::cout << My8x8array[0][1] << std::endl; // ... std::cout << My8x8array[7][6] << std::endl; std::cout << My8x8array[7][7] << std::endl;
Great! Thanks a million!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks