• 0

[MFC] changing size of array at runtime?


Question

9 answers to this question

Recommended Posts

  • 0

I prefer to use STL for things like this when dealing with (personal preference).

example: An array that will hold a bunch of integers would be coded something like this:

#include <vector.h>
#include <iostream.h>
using namespace std; //to make the "No std::"-nazi's happy.

int main(int argc, const char * argv[])
{
 ?bool runLoop = true;
 ?vector myVector<int>;

 ?// First let's add some items to the array
 ?while(runLoop)
 ?{
 ? ?int temp;
 ? ?cout << "Enter a number (-99 quits): " << flush;
 ? ?cin >> temp;
 ? ?myVector.push_back(temp);
 ? ?if (temp == -99) runLoop = false;
 ?}
 ?myVector.pop_back(); ?// this line is needed to erase the "-99" in the last position.
 ?
 ?// lets print out the size of the vector, and then it's contents.
 ?cout << "\n\nThe size of myVector (vector) is: " << myVector.size() << endl << endl<< flush;
 ?for (int i = 0; i < myVector.size(); i++) cout << "element " << i << " is: " myVector[i];
  return 0;
}

Written here so it may not compile exactly as printed but I'm sure you get the idea. Vectors are nice because you can access them like a stanard array (array[index]), you can add/remove items to/from the end with ease, and you can use the STL routines to sort them.

Not as nice as NSMutableArray in Objective-C but it's not too bad either.

  • 0

I've done vector of vectors before, works fine and everything but if you prefer to use c-style memory management you can a)use a resize function (Which i believe is included) b)write your own.

vector< vector<int> > aliens;

//now to give memory to each vector of aliens
//makes something similar to aliens[5][11];
aliens.resize(5);
    for (int n=0;n<5;n++)
        aliens[n].resize(11);

  • 0

thanks, got vectors working now.

but i still cant get it to resize:

i can make it smaller and it works perfectly, however as soon as i try to make it bigger than its orrignal bounds i get a access violation

my code is:

decleration:

std::vector< std::vector<bool> > grid;
	std::vector< std::vector<bool> > newgrid;

resize function:

void Gbg::Resize(int x, int y)
{
	grid.resize(x);
	newgrid.resize(x);	
    for (int i=0;i<x;i++)
	{
  grid[i].resize(y);
  newgrid[i].resize(y);
	}


   for (int j=gx;j<x;j++)
   {
  for (int l = gy; l < y; l++)
  {
 	 grid[j][l] = false;
 	 newgrid[j][l] = false;
  }
   }

	gx = x;
	gy = y;
}

  • 0

try this, its much more elegant anyways. Think of an iterator as a pointer.

this is off top of my head. hope its right. :p

// This resizes it all accordingly

aliens.resize(2000000);
for (vector < vector<CAlien> >::iterator i = aliens.begin(); i <aliens.end(); i++)
{
        i->resize(10000);
}

// This performs an action() on every item in the list

for (vector< vector<CAlien> >::iterator i=aliens.begin(); i < aliens.end(); i++)
{
     for (vector<CAlien>::iterator it = i->begin(); it < i->end(); it++)
     {
               it->action();
     }
}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I like Neowin a lot, but this "article" is shady and misleading. We need an "editorial" flag and this isn't "news" it's "my cousin's dog's best friend posted on twitter about their snapchat account from their dog's owner that a bad thing happened, so it's NEWS." Thanks
    • Burrrrn. Bought COD WWII last month when it was on sale for PC. Oh well. Excellent game tho, and the PC version plays/looks amazing (and has it's own PC achievements). ...wondering if this is a lesson on waiting on sales. ...also hoping Rise of the Tomb Raider has it's own PC Achievements
    • Google Chrome 137.0.7151.120 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Google Chrome 137.0.7151.120 changelog: [$7000][420697404] High CVE-2025-6191: Integer overflow in V8. Reported by Shaheen Fazim on 2025-05-27 [$4000][421471016] High CVE-2025-6192: Use after free in Profiler. Reported by Chaoyuan Peng (@ret2happy) on 2025-05-31 [425443272] Various fixes from internal audits, fuzzing and other initiatives Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | 128.0 MB Download: Google Chrome Offline Installer 32-bit | 115.0 MB Download page: Google Chrome Portable Download: Google Chrome MSI Installers for Windows (automatic update) View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Custom Greek Shirts earned a badge
      One Month Later
    • Week One Done
      Custom Greek Shirts earned a badge
      Week One Done
    • One Year In
      Custom Greek Shirts earned a badge
      One Year In
    • Week One Done
      topantidetectbrowser earned a badge
      Week One Done
    • Explorer
      Jdoe25 went up a rank
      Explorer
  • Popular Contributors

    1. 1
      +primortal
      672
    2. 2
      ATLien_0
      281
    3. 3
      Michael Scrip
      223
    4. 4
      +FloatingFatMan
      190
    5. 5
      Steven P.
      145
  • Tell a friend

    Love Neowin? Tell a friend!