• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
black_darkness

I need help with vectors of vectors.

14 posts in this topic

Hello. I am trying to something like a 2d array of bools (ex: bool var[11][11]). But using vectors instead.



I think that this doesn't work because you cannot put a vector in data type bool.
[CODE]
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<vector<bool>> mv;
int x = 11;
int y = 11;
for(int i=0;i<x;i++) {
mv.push_back(vector<bool>());
cout<<"Creating x["<<mv.size()-1<<"]\n";
for(int j=0;j<y;j++) {
mv[i].push_back(vector<bool>());
}
}
return 0;
}
[/CODE]

But in order to create a dynamic function that takes x and y and creates a 2d vector of bools I have no idea where to begin.
0

Share this post


Link to post
Share on other sites
[quote name='Álvaro' timestamp='1355713829' post='5011480']
What do you mean by "doesn't work"? What error message do you get?
[/quote]

I'm getting this error
error C2664: 'std::vector<_Ty,_Ax>::push_back' : cannot convert parameter 1 from 'std::vector<_Ty,_Ax>' to 'bool'

Btw that error corresponds to line 13. Edited by black_darkness
0

Share this post


Link to post
Share on other sites
In line 13, you should just be pushing a bool, not a vector of bools. You would only push a vector in that inner loop if you were implementing a vector of vectors of vectors of bools (a 3D array, in other words). Edited by JTippetts
2

Share this post


Link to post
Share on other sites
Thank you both. I see the problem now. I was having trouble conceptualizing this. Now I got it. Thank you.

@nobodynews that is a good function. I was planning on creating a function later. Thanks for the good function. Edited by black_darkness
0

Share this post


Link to post
Share on other sites
Here is the finished product.
[CODE]
vector<vector<bool>> mv;
int x = 11;
int y = 11;
for(int i=0;i<x;i++) {
mv.push_back(vector<bool>());
for(int j=0;j<y;j++) {
mv[i].push_back(new bool);
}
}
[/CODE]

Will this handle the garbage collecting?
[CODE]
for (unsigned int i=0;i<mv.size();i++) {
mv[i].erase(mv[i].begin());
}
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='JTippetts' timestamp='1355718473' post='5011518']
mv.clear() would be sufficient to clear everything.
[/quote]

Thank you.

BTW. I found this code just now for visual studio compilers. src http://www.david-amador.com/2010/10/tracking-memory-leaks-in-visual-studio/

It detects memory leaks. I had a ton.
Do you recommend this as a good way for me to test for errors in the future?

[CODE]
#ifdef _DEBUG
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
_CrtSetDbgFlag(flag);
#endif
[/CODE]
2

Share this post


Link to post
Share on other sites
[quote name='black_darkness' timestamp='1355718293' post='5011515']
Here is the finished product.
[CODE]vector<vector<bool>> mv;
int x = 11;
int y = 11;
for(int i=0;i<x;i++) {
mv.push_back(vector<bool>());
for(int j=0;j<y;j++) {
mv[i].push_back(new bool);
}
}[/CODE][/quote]Whoa hold on a sec there. You're dynamically allocating each bool. That is not a "finished product", and is not right at all. You would simply need to push_back the value, i.e. false. It's only compiling due to a pointer being able to be implicity converted to a bool, with the value 'true' in this case since the pointer isn't NULL. So it leaks like a sieve. It's also much longer, and much less efficient in both memory and time taken*, even if you ignore the masses of leaks it causes. Nope, far from being finished, what you actually have there is downright awful.

* The code nobodynews posted performs a copy-construction of each inner vector which results in never having to grow the vector because it starts out at the ideal size. Your code on the other hand causes the vector to grow when it reaches a certain size, and it will grow the capacity to probably larger than you need it for.

Do it the right way. Taking the important bits from nobodynews' code and inserting them into yours produces this:[code]int x = 11, y = 11;
std::vector< std::vector<bool> > mv(x, std::vector<bool>(y, false));[/code]
Now [i]that's[/i] how you do it properly! Edited by iMalc
2

Share this post


Link to post
Share on other sites
[quote name='iMalc' timestamp='1355730128' post='5011578']
[quote name='black_darkness' timestamp='1355718293' post='5011515']
Here is the finished product.
[CODE]vector<vector<bool>> mv;
int x = 11;
int y = 11;
for(int i=0;i<x;i++) {
mv.push_back(vector<bool>());
for(int j=0;j<y;j++) {
mv[i].push_back(new bool);
}
}[/CODE][/quote]Whoa hold on a sec there. You're dynamically allocating each bool. That is not a "finished product", and is not right at all. You would simply need to push_back the value, i.e. false. It's only compiling due to a pointer being able to be implicity converted to a bool, with the value 'true' in this case since the pointer isn't NULL. So it leaks like a sieve. It's also much longer, and much less efficient in both memory and time taken*, even if you ignore the masses of leaks it causes. Nope, far from being finished, what you actually have there is downright awful.

* The code nobodynews posted performs a copy-construction of each inner vector which results in never having to grow the vector because it starts out at the ideal size. Your code on the other hand causes the vector to grow when it reaches a certain size, and it will grow the capacity to probably larger than you need it for.

Do it the right way. Taking the important bits from nobodynews' code and inserting them into yours produces this:[code]int x = 11, y = 11;
std::vector< std::vector<bool> > mv(x, std::vector<bool>(y, false));[/code]
Now [i]that's[/i] how you do it properly!
[/quote]

I feel evil now. Thanks. I was wondering why the program wouldn't work. I got distracted trying to solve my memory leaks and couldn't fix them. Then I came back and saw your post. I appreciate it. It doesn't help that cplusplus.com is down, which is my reference of choice for problems.
0

Share this post


Link to post
Share on other sites
Once you are using vectors, you shouldn't write `new' anywhere: The vector is doing the memory allocations for you. If you feel tempted to use `new' again, ask here to make sure you really have a good reason (hint: unless you are trying to get polymorphic behavior, you won't need it).
2

Share this post


Link to post
Share on other sites
Are you sure you actually want to use a std::vector<bool> ?

http://www.gotw.ca/publications/N1185.pdf
[url="http://www.gotw.ca/publications/N1211.pdf"]http://www.gotw.ca/p...tions/N1211.pdf[/url] Edited by Nanook
0

Share this post


Link to post
Share on other sites
My test I wrote is compiling properly but It crashes.

When it crashes it tells me
Expression: vector<bool> iterator not dereferencable.

And
Standard C++ libraries are out of range &&0

What I am trying to get the test to do is make a class where I can input x,y, and a string. X and Y will determine width and height. And the string will determine if the coordinate is passable or impassable. I keep thinking I solve the problem but I keep getting an error. I am starting to get lost because I keep having to correct mistakes and I forget what mistakes I made. I am getting lost I feel like I am in a vicious cycle. Any help would be appreciated.

[CODE]
vector<vector<bool>> make_map(int x, int y, string grid)
{
vector<vector<bool>> mv(x, vector<bool>(y, false));
int counter = 0;
for(int i=0;i<x;i++) {
for(int j=0;i<y;j++) {
if(grid[counter]=='X') {
mv[i][j]=false;
}
else if(grid[counter]=='O') {
mv[i][j] = true;
}
counter++;
}
}
return mv;
}

int main()
{
int mx = 3;
int my = 3;
string grid = "XOXOOOOXO";
vector<vector<bool>> mv = make_map(mx,my,grid);
for(int i=0;i<mx;i++) {
for(int j=0;j<my;j++) {
cout<<"X: "<<i<<"Y: "<<j<<"= "<<mv[i][j]<<"\n";
}
}
return 0;
}

[/CODE] Edited by black_darkness
0

Share this post


Link to post
Share on other sites
Well I solved this one myself. The problem was on line 6. I had an i instead of a j. Now everything works. I laughed like a maniac. I am very pleased with this. Edited by black_darkness
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0