3 Replies - 2429 Views - Last Post: 12 August 2009 - 02:46 AM Rate Topic: -----

#1 mostlyharmless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-August 09

Casting iterators?

Posted 11 August 2009 - 11:43 AM

Hi!

I am working on a neural network where I have a superclass Neuron that has been extended to InputNeuron, HiddenNeuron and OutputNeuron. In the NeuralNetwork class, I am writing a function where I need to iterate through the different layers of the network. At one point, I check whether or not there are hidden layers (in which case the first of these should be connected to the input layer) or not (in which case the input layer should be directly connected to the output layer). Instead of writing two for-loops, I was thinking that one could do something like this:

	vector<Neuron*>::iterator beg_it, end_it;
	if ( num_layers > 0 ) {
		beg_it = layers[0].begin(); // layers = 
		end_it = layers[0].end();   // vector< vector<HiddenNeuron> >
	}
	else {
		beg_it = outputs.begin();  // outputs =
		end_it = outputs.end();	// vector<OutputNeuron>
	}
	// Connections
	for (beg_it; beg_it < end_it; beg_it++) {
		iss >> weight;
		(**it).addConnection(weight, *beg_it);
	}



Unfortunately, this does not seem to work. I get the following error message for lines 3, 4, 7 and 8:

[tt]> error: no match for ‘operator=’ in ‘end_it = ...[/tt]

I have tried both static and dynamic casts, resulting in:

[tt]> error: no matching function for call to ...[/tt]

I'm not that well-versed in C++, so if I was trying to do something considered blatantly stupid, please explain how things work thoroughly. Any help would be appreciated, I've spent a few hours on Google and posted this on another forum as well, but to no avail.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Casting iterators?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Casting iterators?

Posted 11 August 2009 - 11:50 AM

See the code comments, but basically, the iterator needs to match the data structure its iterating through (layered vect, vect of objects, etc...):

#include <iostream>
#include <vector>
using namespace std;

int main() {

	//regular vector:
	vector<int> singleVect;
	//vector inside a vector:
	vector<vector<int>> layeredVect;

	//both have different storage, so different iterators will be needed
	//if I try to assign an itertor of the first vector to a value of
	//the second, an error will come up

	//example, giving the vectors some data

	for(int i = 0; i < 5; i++) {
		singleVect.push_back(i+2);
	}

	//add our vector to the layered one
	layeredVect.push_back(singleVect);

	//iterators
	vector<int>::iterator singleIter;

	singleIter = layeredVect[0].begin(); //OK!

	//singleIter = layeredVect.begin();// BAD!

	for(;singleIter != layeredVect[0].end(); singleIter++) {
		cout << *singleIter << endl;
	}
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 mostlyharmless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-August 09

Re: Casting iterators?

Posted 11 August 2009 - 05:29 PM

Hi KYA!

Thank you very much for your quick reply! I was aware of the fact that a vector<T> is not the same as vector<vector<T>> and iterators of these will not match. To rephrase my question:

A is a superclass
B extends A

vector<B> bVect;

vector<A>::iterator aIter;

aIter = bVect.begin(); // Doesn't work

Can I somehow cast the vector<B>::iterator to a vector<A>::iterator?
Was This Post Helpful? 0
  • +
  • -

#4 mostlyharmless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-August 09

Re: Casting iterators?

Posted 12 August 2009 - 02:46 AM

Ok, there was no way to do that, there were only two solutions:

1. Write two for-loops
2. Make the vectors (layers[i], outputs) vector<Neuron*> vectors. In the second example, bVect would have to be vector<A*>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1