iterator

[Vector] Iterator and 2d vector

vector< vector<int> >::iterator temp = mincost.end(); vector<int> a = *temp; if ( *temp != *(temp--) ) return 0; mincost is a 2d vector, I want to get the last vector<int> of this vector and last--. I don't really understand about iterator :) . Help me !! :D Thx ^^ ...

C# 2.0 - Is there any way to do a `GroupBy` with a yielded itterator block?

I'm working with a C# 2.0 app so linq/lambda answers will be no help here. Basically I'm faced with a situation where i need to yield return an object but only if one if it's properties is unique (Group By). For example,..say i have a collection of users and i want a grouped collection based on name (i might have 20 Daves but I'd only ...

"error: assignment of read-only location" in unordered_map (C++)

I have an awkward hash table (specifically, an unordered_map) with int keys and vector< vector< int >> data. I periodically need to update elements in this two-dimensional vector of ints. There's no intrinsic reason I shouldn't be able to, right? A newer g++ compiler I've switched to complains of an assignment of read-only location on th...

Anybody know why the Output of this program is like this?(using iterator in c#)

using System; using System.Collections; namespace Iterator_test { class Day { int days_idx = -1; private String[] days = { "mon", "tue", "wed","thu","fri","sat","sun" }; public IEnumerable getdays() { days_idx++; yield return days[days_idx]; } } class Program { static void Main(string[] args) ...

How to insert into nested vector without invalidating iterator(s)

I have some boolean expressions to evaluate and process. Maybe this would have all been better with Boost, but I'm still learning STL and didn't go that way. I'm now learning about iterator validation, or INvalidation as the case may be. Is there a way to insert a new element into this nested vector below safely? If you don't want t...

Is it possible to use a RA-iterator out of range?

Consider the following code: typedef std::vector<int> cont_t; // Any container with RA-iterators typedef cont_t::const_iterator citer_t; // Random access iterator cont_t v(100); const int start = 15; // start > 0. citer_t it = v.begin() - start; // Do not use *it int a1 = 20, b1 = 30; // a1, b1 >= start int a2 = 30, b2 = 40; // a2, b2...

invalid initialization of non-const reference of type ‘int&' from a temporary of type 'MyClass<int>::iterator*'

I'm getting the following error from g++ while trying to add iterator support for my linked list class. LinkedList.hpp: In member function ‘Type& exscape::LinkedList::iterator::operator*() [with Type = int]’: tests.cpp:51: instantiated from here LinkedList.hpp:412: error: invalid initialization of non-const reference of type ‘int&’ fr...

Checking for list membership using the STL and a unary function adapted functor

I've attempted to write a brief utility functor that takes two std::pair items and tests for their equality, but disregarding the ordering of the elements. Additionally (and this is where I run into trouble) I've written a function to take a container of those std::pair items and test for membership of a given pair argument in a the cont...

How do I apply the DRY principle to iterators in C++? (iterator, const_iterator, reverse_iterator, const_reverse_iterator)

OK, so I have two (completely unrelated, different project) classes using iterators now. One has iterator and reverse_iterator working as intended, and the other, current one has iterator and a semi-broken const_iterator (specifically, because const_iterator derives from iterator, the code LinkedList<int>::iterator i = const_list.begin()...

Unable to correct error in C++ code

Hi all, I am unable to detect the error in the following C++ program. The code defines a Graph class. #include <vector> #include <list> using namespace std; class Neighbor { public: int node_id; float edge_cost; float price; Neighbor(int&,float&,float&); }; Neighbor::Neighbor(int& node_id, float& edge_cost,float& price) { ...

How do I use __getitem__ and __iter__ and return values from a dictionary ?

I have an object with a dictionary that I want to access via __getitem__ as well as iterate over (values only, keys don't matter) but am not sure how to do it. For example: Python 2.5.2 (r252:60911, Jul 22 2009, 15:33:10) >>> class Library(object): ... def __init__(self): ... self.books = { 'title' : object, 'title2' : object, '...

Create a new Tuple with one element modified

(I am working interactively with a WordprocessingDocument object in IronPython using the OpenXML SDK, but this is really a general Python question that should be applicable across all implementations) I am trying to scrape out some tables from a number of Word documents. For each table, I have an iterator that is giving me table row ob...

iterator for 2d vector

How to create iterator/s for 2d vector (a vector of vectors)? ...

How do I iterate through instances of a class in C#?

Is there a way to iterate over instances of a class in C#? These instances are not tracked or managed in a collection. ...

STL iterator as return value.

I have class A, that contains std::vector and I would like to give an access to the vector from outside class A. The first thing that came to my mind is to make a get function that returns iterator to the vector, but walk through the vector I will need two iterators (begin and end). I was wondering is there any way (technique or patter...

iterator dereferencing issue

if I have list<NS*> v; typename list<NS*>::iterator it; for(it = v.begin();it!=v.end();++it){ cout<<**it.ns_member1<<endl; // does not compile NS ns = **it; cout<<ns.ns_member1<<endl; // this compiles. } Why so? ...

Get a reverse iterator from a forward iterator without knowing the value type

I'm trying to implement some STL-style sorting algorithms. The prototype for std::sort looks something like this (from cplusplus.com): template <class RandomAccessIterator> void sort ( RandomAccessIterator first, RandomAccessIterator last ); The function is generally called like this (although the container type can vary): std::vecto...

Pointer for item in iteration over std::list

I'm working on a very basic game and I have a std::list collection of objects that pertain to my game. I declared it as: std::list<Target> targets; When I iterate over it, using for (std::list<Target>::iterator iter = targets.begin(); iter != targets.end(); iter++) { Target t = *iter; t.move(); } My objects are...

std::map find doesn't work properly

std::map.find() is intended to return an map::iterator to an element it found if there is some or to the end() if not. I get BadPtr returned to me. The exactly same construct works fine in the other part of code. What is this? class OntologyContainer { map<string, OntologyClass*> data; OntologyClass* last_added; public: clas...

How to create a generator/iterator with the Python C API?

How do I replicate the following Python code with the Python C API? class Sequence(): def __init__(self, max): self.max = max def data(self): i = 0 while i < self.max: yield i i += 1 So far, I have this: #include <Python/Python.h> #include <Python/structmember.h> /* Define a ne...