2D iterator for loop
I'm having issues implementing an iterator class in regards to .end(). The problem I'm running into is when I try to use a for loop with my iterator class; it's currently stopping just before the last element in a 2D vector. However, I want it to go one element past the last element without causing a compiler error, and to include the last character in the print statement.
main.cpp
// file contents
// aaa
// bbb
// cce
// factory method (adds file contents to 2D <char> vector)
Base *a = Base::create("file");
cout << *a; // overloaded '<<'
Prints
a a a b b b c c e
Now, when I use a for loop with my iterator class, it doesn't include the last character.
for(auto it = a->begin(); it != a->end(); it++)
cout << *it << ' ';
Prints
a a a b b b c c
The .end prints the following
Base::iterator it = aa->end();
cout << *it << 'n';
// prints e
When I try a while loop, it includes the last character.
// Note: my iterator class is a nested class inside Base class.
const Base::iterator et = a->begin();
int i = 0;
while(i < 13) {
cout << *et << ' ';
et++;
}
Prints
a a a b b b c c e e e e e
I understand that a->end() is supposed to point just past the last character, but I don't understand how to implement that. When I increment past the last value in my operator++(int), it displays a segmentation fault. Currently, my overloaded increment method stops at the last character and doesn't go past it. With that said, how do I implement my ++(int) method to include the last element when printing from a for loop? Am I supposed to add a null element to the vector or something like that?
Inside Base.cpp
// This function is whats causing the issue. I know it looks ugly.
Base::iterator Base::iterator::operator++(int) {
// base is a Base *, x and y are coordinates for 2D vector
Base::iterator temp(base, x, y); // save value
// if iterator has not reached end
if( !((x == base->vec.size()-1) && (y == base->vec[0].size()-1)) )
{
// if y < row size
if(y < base->vec[0].size()-1)
y++;
// if y has reached end of row, increment x and start y on a new row
else if(x < base->vec.size() && y == base->vec[0].size()-1) {
y=0;
x++;
}
}
return temp;
}
Base::iterator Base::begin() {
return Base::iterator(this, 0, 0);
}
Base::iterator Base::end() {
return Base::iterator(this, vec.size()-1, vec[0].size()-1);
}
Rest of Base.cpp
#include "Base.h"
using namespace std;
// Factory method (instantiates 2D vector with file contents)
Base *Base::create(string filename) {/*removed irrelevant code */}
Base::~Base(){}
// prints 2D vector
ostream &operator<<(ostream &os, const Base &val){/*removed irrelevant code*/}
Base::iterator::iterator(Base *b, int m, int n): base(b), x(m), y(n) {}
Base::iterator::~iterator(){}
// returns a character inside 2D vector
char &Base::iterator::operator*() const {
return base->vec[x][y];
}
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base->vec[x][y] == *rhs;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return base->vec[x][y] != *rhs;
}
// Bunch of other functions
Any help would be appreciated.
c++ iterator
add a comment |
I'm having issues implementing an iterator class in regards to .end(). The problem I'm running into is when I try to use a for loop with my iterator class; it's currently stopping just before the last element in a 2D vector. However, I want it to go one element past the last element without causing a compiler error, and to include the last character in the print statement.
main.cpp
// file contents
// aaa
// bbb
// cce
// factory method (adds file contents to 2D <char> vector)
Base *a = Base::create("file");
cout << *a; // overloaded '<<'
Prints
a a a b b b c c e
Now, when I use a for loop with my iterator class, it doesn't include the last character.
for(auto it = a->begin(); it != a->end(); it++)
cout << *it << ' ';
Prints
a a a b b b c c
The .end prints the following
Base::iterator it = aa->end();
cout << *it << 'n';
// prints e
When I try a while loop, it includes the last character.
// Note: my iterator class is a nested class inside Base class.
const Base::iterator et = a->begin();
int i = 0;
while(i < 13) {
cout << *et << ' ';
et++;
}
Prints
a a a b b b c c e e e e e
I understand that a->end() is supposed to point just past the last character, but I don't understand how to implement that. When I increment past the last value in my operator++(int), it displays a segmentation fault. Currently, my overloaded increment method stops at the last character and doesn't go past it. With that said, how do I implement my ++(int) method to include the last element when printing from a for loop? Am I supposed to add a null element to the vector or something like that?
Inside Base.cpp
// This function is whats causing the issue. I know it looks ugly.
Base::iterator Base::iterator::operator++(int) {
// base is a Base *, x and y are coordinates for 2D vector
Base::iterator temp(base, x, y); // save value
// if iterator has not reached end
if( !((x == base->vec.size()-1) && (y == base->vec[0].size()-1)) )
{
// if y < row size
if(y < base->vec[0].size()-1)
y++;
// if y has reached end of row, increment x and start y on a new row
else if(x < base->vec.size() && y == base->vec[0].size()-1) {
y=0;
x++;
}
}
return temp;
}
Base::iterator Base::begin() {
return Base::iterator(this, 0, 0);
}
Base::iterator Base::end() {
return Base::iterator(this, vec.size()-1, vec[0].size()-1);
}
Rest of Base.cpp
#include "Base.h"
using namespace std;
// Factory method (instantiates 2D vector with file contents)
Base *Base::create(string filename) {/*removed irrelevant code */}
Base::~Base(){}
// prints 2D vector
ostream &operator<<(ostream &os, const Base &val){/*removed irrelevant code*/}
Base::iterator::iterator(Base *b, int m, int n): base(b), x(m), y(n) {}
Base::iterator::~iterator(){}
// returns a character inside 2D vector
char &Base::iterator::operator*() const {
return base->vec[x][y];
}
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base->vec[x][y] == *rhs;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return base->vec[x][y] != *rhs;
}
// Bunch of other functions
Any help would be appreciated.
c++ iterator
If you're gonna wrap a 2D-vector it's probably better to use a 1D vector in the underlying implementation.
– super
Nov 20 '18 at 22:36
Ya, I thought about that, but that would require completely redoing all my methods to work with a 1D vector and adding additional class variables. Worst case, I may end up doing it. Just looking to see if there is any solution for 2D iterators.
– N. Colostate
Nov 20 '18 at 22:44
add a comment |
I'm having issues implementing an iterator class in regards to .end(). The problem I'm running into is when I try to use a for loop with my iterator class; it's currently stopping just before the last element in a 2D vector. However, I want it to go one element past the last element without causing a compiler error, and to include the last character in the print statement.
main.cpp
// file contents
// aaa
// bbb
// cce
// factory method (adds file contents to 2D <char> vector)
Base *a = Base::create("file");
cout << *a; // overloaded '<<'
Prints
a a a b b b c c e
Now, when I use a for loop with my iterator class, it doesn't include the last character.
for(auto it = a->begin(); it != a->end(); it++)
cout << *it << ' ';
Prints
a a a b b b c c
The .end prints the following
Base::iterator it = aa->end();
cout << *it << 'n';
// prints e
When I try a while loop, it includes the last character.
// Note: my iterator class is a nested class inside Base class.
const Base::iterator et = a->begin();
int i = 0;
while(i < 13) {
cout << *et << ' ';
et++;
}
Prints
a a a b b b c c e e e e e
I understand that a->end() is supposed to point just past the last character, but I don't understand how to implement that. When I increment past the last value in my operator++(int), it displays a segmentation fault. Currently, my overloaded increment method stops at the last character and doesn't go past it. With that said, how do I implement my ++(int) method to include the last element when printing from a for loop? Am I supposed to add a null element to the vector or something like that?
Inside Base.cpp
// This function is whats causing the issue. I know it looks ugly.
Base::iterator Base::iterator::operator++(int) {
// base is a Base *, x and y are coordinates for 2D vector
Base::iterator temp(base, x, y); // save value
// if iterator has not reached end
if( !((x == base->vec.size()-1) && (y == base->vec[0].size()-1)) )
{
// if y < row size
if(y < base->vec[0].size()-1)
y++;
// if y has reached end of row, increment x and start y on a new row
else if(x < base->vec.size() && y == base->vec[0].size()-1) {
y=0;
x++;
}
}
return temp;
}
Base::iterator Base::begin() {
return Base::iterator(this, 0, 0);
}
Base::iterator Base::end() {
return Base::iterator(this, vec.size()-1, vec[0].size()-1);
}
Rest of Base.cpp
#include "Base.h"
using namespace std;
// Factory method (instantiates 2D vector with file contents)
Base *Base::create(string filename) {/*removed irrelevant code */}
Base::~Base(){}
// prints 2D vector
ostream &operator<<(ostream &os, const Base &val){/*removed irrelevant code*/}
Base::iterator::iterator(Base *b, int m, int n): base(b), x(m), y(n) {}
Base::iterator::~iterator(){}
// returns a character inside 2D vector
char &Base::iterator::operator*() const {
return base->vec[x][y];
}
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base->vec[x][y] == *rhs;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return base->vec[x][y] != *rhs;
}
// Bunch of other functions
Any help would be appreciated.
c++ iterator
I'm having issues implementing an iterator class in regards to .end(). The problem I'm running into is when I try to use a for loop with my iterator class; it's currently stopping just before the last element in a 2D vector. However, I want it to go one element past the last element without causing a compiler error, and to include the last character in the print statement.
main.cpp
// file contents
// aaa
// bbb
// cce
// factory method (adds file contents to 2D <char> vector)
Base *a = Base::create("file");
cout << *a; // overloaded '<<'
Prints
a a a b b b c c e
Now, when I use a for loop with my iterator class, it doesn't include the last character.
for(auto it = a->begin(); it != a->end(); it++)
cout << *it << ' ';
Prints
a a a b b b c c
The .end prints the following
Base::iterator it = aa->end();
cout << *it << 'n';
// prints e
When I try a while loop, it includes the last character.
// Note: my iterator class is a nested class inside Base class.
const Base::iterator et = a->begin();
int i = 0;
while(i < 13) {
cout << *et << ' ';
et++;
}
Prints
a a a b b b c c e e e e e
I understand that a->end() is supposed to point just past the last character, but I don't understand how to implement that. When I increment past the last value in my operator++(int), it displays a segmentation fault. Currently, my overloaded increment method stops at the last character and doesn't go past it. With that said, how do I implement my ++(int) method to include the last element when printing from a for loop? Am I supposed to add a null element to the vector or something like that?
Inside Base.cpp
// This function is whats causing the issue. I know it looks ugly.
Base::iterator Base::iterator::operator++(int) {
// base is a Base *, x and y are coordinates for 2D vector
Base::iterator temp(base, x, y); // save value
// if iterator has not reached end
if( !((x == base->vec.size()-1) && (y == base->vec[0].size()-1)) )
{
// if y < row size
if(y < base->vec[0].size()-1)
y++;
// if y has reached end of row, increment x and start y on a new row
else if(x < base->vec.size() && y == base->vec[0].size()-1) {
y=0;
x++;
}
}
return temp;
}
Base::iterator Base::begin() {
return Base::iterator(this, 0, 0);
}
Base::iterator Base::end() {
return Base::iterator(this, vec.size()-1, vec[0].size()-1);
}
Rest of Base.cpp
#include "Base.h"
using namespace std;
// Factory method (instantiates 2D vector with file contents)
Base *Base::create(string filename) {/*removed irrelevant code */}
Base::~Base(){}
// prints 2D vector
ostream &operator<<(ostream &os, const Base &val){/*removed irrelevant code*/}
Base::iterator::iterator(Base *b, int m, int n): base(b), x(m), y(n) {}
Base::iterator::~iterator(){}
// returns a character inside 2D vector
char &Base::iterator::operator*() const {
return base->vec[x][y];
}
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base->vec[x][y] == *rhs;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return base->vec[x][y] != *rhs;
}
// Bunch of other functions
Any help would be appreciated.
c++ iterator
c++ iterator
edited Nov 20 '18 at 23:12
N. Colostate
asked Nov 20 '18 at 22:25
N. ColostateN. Colostate
356
356
If you're gonna wrap a 2D-vector it's probably better to use a 1D vector in the underlying implementation.
– super
Nov 20 '18 at 22:36
Ya, I thought about that, but that would require completely redoing all my methods to work with a 1D vector and adding additional class variables. Worst case, I may end up doing it. Just looking to see if there is any solution for 2D iterators.
– N. Colostate
Nov 20 '18 at 22:44
add a comment |
If you're gonna wrap a 2D-vector it's probably better to use a 1D vector in the underlying implementation.
– super
Nov 20 '18 at 22:36
Ya, I thought about that, but that would require completely redoing all my methods to work with a 1D vector and adding additional class variables. Worst case, I may end up doing it. Just looking to see if there is any solution for 2D iterators.
– N. Colostate
Nov 20 '18 at 22:44
If you're gonna wrap a 2D-vector it's probably better to use a 1D vector in the underlying implementation.
– super
Nov 20 '18 at 22:36
If you're gonna wrap a 2D-vector it's probably better to use a 1D vector in the underlying implementation.
– super
Nov 20 '18 at 22:36
Ya, I thought about that, but that would require completely redoing all my methods to work with a 1D vector and adding additional class variables. Worst case, I may end up doing it. Just looking to see if there is any solution for 2D iterators.
– N. Colostate
Nov 20 '18 at 22:44
Ya, I thought about that, but that would require completely redoing all my methods to work with a 1D vector and adding additional class variables. Worst case, I may end up doing it. Just looking to see if there is any solution for 2D iterators.
– N. Colostate
Nov 20 '18 at 22:44
add a comment |
1 Answer
1
active
oldest
votes
Base::iterator(this, vec.size()-1, vec[0].size()-1);
this will return a valid last element. So you need to change it to Base::iterator(this, vec.size(), 0);
, and also update the conditions to switch to a new row in your loop.
Something like:
// if iterator has not reached end
if(x < base->vec.size())
{
++y;
// if y has reached end of row, increment x and start y on a new row
if(y >= base->vec[0].size() {
y=0;
++x;
}
}
Also the iterators are wrong:
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base == rhs.base && x == rhs.x && y == ris.y;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return !(base == rhs.base && x == rhs.x && y == ris.y);
}
How do I resolve the segmentation fault? return Base::iterator(this, vec.size(), vec[0].size()); displays a segmentation fault. (3, 3) is out of bounds. The 2D vector only has indices (0,0) -> (2,2).
– N. Colostate
Nov 20 '18 at 22:38
1
Oops, bad bound, x < size() and bad bound for the end iterator, should be size(), 0.
– Matthieu Brucher
Nov 20 '18 at 22:46
Still gives a segmentation fault. Maybe I have to do something with my iterator class constructor? Thats where its currently flagging it as a segmentation fault. It's saying it can't assign values (3,0) to base*[x][y] since its out of bounds.
– N. Colostate
Nov 20 '18 at 22:53
It has probably to do with your comparison operators. Just compare base, x, y. You can't dereference end, as it's always a non existing object.
– Matthieu Brucher
Nov 20 '18 at 22:54
1
Compare base, x and y, don't compare the values themselves, they could even be the same for different iterators. Iterators compare "pointers", not the pointed values.
– Matthieu Brucher
Nov 20 '18 at 23:37
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402513%2f2d-iterator-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Base::iterator(this, vec.size()-1, vec[0].size()-1);
this will return a valid last element. So you need to change it to Base::iterator(this, vec.size(), 0);
, and also update the conditions to switch to a new row in your loop.
Something like:
// if iterator has not reached end
if(x < base->vec.size())
{
++y;
// if y has reached end of row, increment x and start y on a new row
if(y >= base->vec[0].size() {
y=0;
++x;
}
}
Also the iterators are wrong:
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base == rhs.base && x == rhs.x && y == ris.y;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return !(base == rhs.base && x == rhs.x && y == ris.y);
}
How do I resolve the segmentation fault? return Base::iterator(this, vec.size(), vec[0].size()); displays a segmentation fault. (3, 3) is out of bounds. The 2D vector only has indices (0,0) -> (2,2).
– N. Colostate
Nov 20 '18 at 22:38
1
Oops, bad bound, x < size() and bad bound for the end iterator, should be size(), 0.
– Matthieu Brucher
Nov 20 '18 at 22:46
Still gives a segmentation fault. Maybe I have to do something with my iterator class constructor? Thats where its currently flagging it as a segmentation fault. It's saying it can't assign values (3,0) to base*[x][y] since its out of bounds.
– N. Colostate
Nov 20 '18 at 22:53
It has probably to do with your comparison operators. Just compare base, x, y. You can't dereference end, as it's always a non existing object.
– Matthieu Brucher
Nov 20 '18 at 22:54
1
Compare base, x and y, don't compare the values themselves, they could even be the same for different iterators. Iterators compare "pointers", not the pointed values.
– Matthieu Brucher
Nov 20 '18 at 23:37
|
show 3 more comments
Base::iterator(this, vec.size()-1, vec[0].size()-1);
this will return a valid last element. So you need to change it to Base::iterator(this, vec.size(), 0);
, and also update the conditions to switch to a new row in your loop.
Something like:
// if iterator has not reached end
if(x < base->vec.size())
{
++y;
// if y has reached end of row, increment x and start y on a new row
if(y >= base->vec[0].size() {
y=0;
++x;
}
}
Also the iterators are wrong:
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base == rhs.base && x == rhs.x && y == ris.y;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return !(base == rhs.base && x == rhs.x && y == ris.y);
}
How do I resolve the segmentation fault? return Base::iterator(this, vec.size(), vec[0].size()); displays a segmentation fault. (3, 3) is out of bounds. The 2D vector only has indices (0,0) -> (2,2).
– N. Colostate
Nov 20 '18 at 22:38
1
Oops, bad bound, x < size() and bad bound for the end iterator, should be size(), 0.
– Matthieu Brucher
Nov 20 '18 at 22:46
Still gives a segmentation fault. Maybe I have to do something with my iterator class constructor? Thats where its currently flagging it as a segmentation fault. It's saying it can't assign values (3,0) to base*[x][y] since its out of bounds.
– N. Colostate
Nov 20 '18 at 22:53
It has probably to do with your comparison operators. Just compare base, x, y. You can't dereference end, as it's always a non existing object.
– Matthieu Brucher
Nov 20 '18 at 22:54
1
Compare base, x and y, don't compare the values themselves, they could even be the same for different iterators. Iterators compare "pointers", not the pointed values.
– Matthieu Brucher
Nov 20 '18 at 23:37
|
show 3 more comments
Base::iterator(this, vec.size()-1, vec[0].size()-1);
this will return a valid last element. So you need to change it to Base::iterator(this, vec.size(), 0);
, and also update the conditions to switch to a new row in your loop.
Something like:
// if iterator has not reached end
if(x < base->vec.size())
{
++y;
// if y has reached end of row, increment x and start y on a new row
if(y >= base->vec[0].size() {
y=0;
++x;
}
}
Also the iterators are wrong:
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base == rhs.base && x == rhs.x && y == ris.y;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return !(base == rhs.base && x == rhs.x && y == ris.y);
}
Base::iterator(this, vec.size()-1, vec[0].size()-1);
this will return a valid last element. So you need to change it to Base::iterator(this, vec.size(), 0);
, and also update the conditions to switch to a new row in your loop.
Something like:
// if iterator has not reached end
if(x < base->vec.size())
{
++y;
// if y has reached end of row, increment x and start y on a new row
if(y >= base->vec[0].size() {
y=0;
++x;
}
}
Also the iterators are wrong:
bool Base::iterator::operator==(const Base::iterator& rhs) const {
return base == rhs.base && x == rhs.x && y == ris.y;
}
bool Base::iterator::operator!=(const Base::iterator& rhs) const {
return !(base == rhs.base && x == rhs.x && y == ris.y);
}
edited Nov 20 '18 at 23:39
answered Nov 20 '18 at 22:32
Matthieu BrucherMatthieu Brucher
15.2k32140
15.2k32140
How do I resolve the segmentation fault? return Base::iterator(this, vec.size(), vec[0].size()); displays a segmentation fault. (3, 3) is out of bounds. The 2D vector only has indices (0,0) -> (2,2).
– N. Colostate
Nov 20 '18 at 22:38
1
Oops, bad bound, x < size() and bad bound for the end iterator, should be size(), 0.
– Matthieu Brucher
Nov 20 '18 at 22:46
Still gives a segmentation fault. Maybe I have to do something with my iterator class constructor? Thats where its currently flagging it as a segmentation fault. It's saying it can't assign values (3,0) to base*[x][y] since its out of bounds.
– N. Colostate
Nov 20 '18 at 22:53
It has probably to do with your comparison operators. Just compare base, x, y. You can't dereference end, as it's always a non existing object.
– Matthieu Brucher
Nov 20 '18 at 22:54
1
Compare base, x and y, don't compare the values themselves, they could even be the same for different iterators. Iterators compare "pointers", not the pointed values.
– Matthieu Brucher
Nov 20 '18 at 23:37
|
show 3 more comments
How do I resolve the segmentation fault? return Base::iterator(this, vec.size(), vec[0].size()); displays a segmentation fault. (3, 3) is out of bounds. The 2D vector only has indices (0,0) -> (2,2).
– N. Colostate
Nov 20 '18 at 22:38
1
Oops, bad bound, x < size() and bad bound for the end iterator, should be size(), 0.
– Matthieu Brucher
Nov 20 '18 at 22:46
Still gives a segmentation fault. Maybe I have to do something with my iterator class constructor? Thats where its currently flagging it as a segmentation fault. It's saying it can't assign values (3,0) to base*[x][y] since its out of bounds.
– N. Colostate
Nov 20 '18 at 22:53
It has probably to do with your comparison operators. Just compare base, x, y. You can't dereference end, as it's always a non existing object.
– Matthieu Brucher
Nov 20 '18 at 22:54
1
Compare base, x and y, don't compare the values themselves, they could even be the same for different iterators. Iterators compare "pointers", not the pointed values.
– Matthieu Brucher
Nov 20 '18 at 23:37
How do I resolve the segmentation fault? return Base::iterator(this, vec.size(), vec[0].size()); displays a segmentation fault. (3, 3) is out of bounds. The 2D vector only has indices (0,0) -> (2,2).
– N. Colostate
Nov 20 '18 at 22:38
How do I resolve the segmentation fault? return Base::iterator(this, vec.size(), vec[0].size()); displays a segmentation fault. (3, 3) is out of bounds. The 2D vector only has indices (0,0) -> (2,2).
– N. Colostate
Nov 20 '18 at 22:38
1
1
Oops, bad bound, x < size() and bad bound for the end iterator, should be size(), 0.
– Matthieu Brucher
Nov 20 '18 at 22:46
Oops, bad bound, x < size() and bad bound for the end iterator, should be size(), 0.
– Matthieu Brucher
Nov 20 '18 at 22:46
Still gives a segmentation fault. Maybe I have to do something with my iterator class constructor? Thats where its currently flagging it as a segmentation fault. It's saying it can't assign values (3,0) to base*[x][y] since its out of bounds.
– N. Colostate
Nov 20 '18 at 22:53
Still gives a segmentation fault. Maybe I have to do something with my iterator class constructor? Thats where its currently flagging it as a segmentation fault. It's saying it can't assign values (3,0) to base*[x][y] since its out of bounds.
– N. Colostate
Nov 20 '18 at 22:53
It has probably to do with your comparison operators. Just compare base, x, y. You can't dereference end, as it's always a non existing object.
– Matthieu Brucher
Nov 20 '18 at 22:54
It has probably to do with your comparison operators. Just compare base, x, y. You can't dereference end, as it's always a non existing object.
– Matthieu Brucher
Nov 20 '18 at 22:54
1
1
Compare base, x and y, don't compare the values themselves, they could even be the same for different iterators. Iterators compare "pointers", not the pointed values.
– Matthieu Brucher
Nov 20 '18 at 23:37
Compare base, x and y, don't compare the values themselves, they could even be the same for different iterators. Iterators compare "pointers", not the pointed values.
– Matthieu Brucher
Nov 20 '18 at 23:37
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402513%2f2d-iterator-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you're gonna wrap a 2D-vector it's probably better to use a 1D vector in the underlying implementation.
– super
Nov 20 '18 at 22:36
Ya, I thought about that, but that would require completely redoing all my methods to work with a 1D vector and adding additional class variables. Worst case, I may end up doing it. Just looking to see if there is any solution for 2D iterators.
– N. Colostate
Nov 20 '18 at 22:44