Hello programmers! I have been working on STL algorithms for some time now as a beginner, and I started doing an exercise, among which is the task of `use the fill algorithm to fill the entire array of strings named items with "hello"`. My code for this is below: // Various statements in exercise in using STL Algorithms #include <iostream> #include <algorithm> #include <iterator> #include <array> using namespace std; int main() { //various statements in using STL algorithms ostream_iterator<int> output(cout," "); //iterator const size_t SIZE=10; array<string,SIZE> myStrings; //array of empty strings fill(myStrings.begin(),myStrings.end(),"hello"); //using the fill algorithm to fill the entire …

Member Avatar
Member Avatar
+0 forum 6

Hello everybody. I'm trying to write program with vectors and iterators. Coordinates X and Y are entered from keyboard (as structure) and placed in vector of points. It's necessary to display coordinates with maximal X and Y. This code was compiled but not display maximal X. What should I change? #include <iostream.h> #include <conio.h> #include <vector.h> #include <math.h> struct points { float x, y; }; int main() { using namespace std; int i; int max; vector<points>pointvect; //vector declaration points point; cout << "Enter a dimension of vectors:\n"; cin >> i; for (int k = 0; k <= i; k++) { …

Member Avatar
Member Avatar
+0 forum 3

Hello funs2code! I have the following problem. I need to count the number of occurrences of a concrete word in a text file. I've tried to count the number of occurrences of each word at first. The following code has no errors but it isn't working! When I type in a command line "project1.exe test.dat cat dog cat dog" the empty string is showed. I've cheked if-statements -> right working. Then I've made a conclusion that problem is in for-statement (with iterators). Can anyone explain me, guys? #include <iostream.h> #include <fstream.h> #include <map.h> #include <string.h> #include <iterator.h> typedef std::map<std::string, int> …

Member Avatar
Member Avatar
+0 forum 5

#include <memory> #include <deque> void MyFunc { std::deque< std::shared_ptr< Sprite* > > SpriteList; func_to_fill_deque( SpriteList ); for( auto i = SpriteList.begin(), end = SpriteList.end(); i != end; i++ ) { SpriteList[ i ]->spriteFunc(); // doesn't work SpriteList.at( i )->spriteFunc(); // also doesn't work } } void func_to_fill_deque( std::deque< std::shared_ptr< Sprite* > >& list ) { ptr = new Sprite( filename ); list.push_back( std::make_shared< Sprite* >( ptr ) ); } When I try SpriteList[ i ] I get a red squiggle under the first '[' with the msg "No operator matches these operands" Trying SpriteList.at( i ) gives a red squiggle …

Member Avatar
Member Avatar
+0 forum 2

public class SongIterator<T,L> implements Iterator<Song<T, L>> {}

Member Avatar
Member Avatar
+0 forum 4

I'm implementing a generi method that's supposed to traverse a binary tree in PostOrder but when I attempt to use it, the Iterator doesn't work as It should when I attempt to use ot on a Binary Tree, can anyone spot my mistake in the code? Thanks. The method makes use of a generic POIterator class which implements a generic Iterator<T> here's th POIterator class public class POIterator<T> implements Iterator<T>{ Deque<BNode<E>>someStack = new LinkedList<BNNode<E>>(); BNode<E> position; public T next(){ position = position.pop(); if(!position.hasLeftNode()) return position.getInfo(); else{ position = position.getLeftNode(); somestack.push(position);} next();//recurisive call position = position.pop(); if(!position.hasRightNode()) next();//recursive call }//end of …

Member Avatar
Member Avatar
+0 forum 2

i'm not sure if a pointer can do the job of an iterator, or otherwise, or they are different things?

Member Avatar
Member Avatar
+0 forum 3

I know I am probably over thinking this but I cannot seem to figure this out. I am trying to check if the value is null and if not to continue. /** * */ public Item getItem(String theItem) { Iterator iter = myItems.iterator(); while(iter.hasNext()) { Item anItem = (Item) iter.next(); if(anItem.getName().equals(theItem)) { return anItem; } } if(anItem == null) { continue; } return null; }

Member Avatar
Member Avatar
+0 forum 4

// Expect to print values of arr[0] to arr[4] #include <iostream> #include <vector> using namespace std; int main() { vector<int *> ptrList; int arr[] = { 0, 10, 20, 30, 40 }; int * ptrArr = arr; for( int i=0; i< 5; i++ ) { ptrList.push_back( ptrArr+i ); } vector<int *>::iterator itr = ptrList.begin(); for( int i=1; i<= 5; i++ ) { cout << **itr-1 << endl; itr++; } return 0; } somebody help me please...dunno where i went wrong :(

Member Avatar
Member Avatar
+0 forum 7

So I was asked to solve this question...however when i compile it, it doesn't work...could somebody tell me what's wrong and how to solve it (im guessing it's something to do with overloading since my lecturer was talking abt it just before this was given)? thanks a lot! #include <iostream> #include <vector> using namespace std; class Integer { public: int value; }; int main() { vector<Integer> v; for( int i=0; i<10; i++ ) { Integer x; x.value = i; v.push_back( x ); } vector<Integer>::iterator itr; for( itr=v.begin(); itr!=v.end(); itr+=2 ) cout << *itr << endl; return 0; }

Member Avatar
Member Avatar
+0 forum 2

Hello everyone! I would like your help regarding this: I have a tuple vector which i must iterate through, and accessing the vector data to decide wether to erase or not the current vector element. My code : typedef tuple <int, CString, int, int> pc_data; vector <pc_data> pc_vec; //Assume that pc_vec is filled with elements somewhere here... //This code is not working. //Exception : vector subject out of range because of the size dynamically changing i suppose for (int i=0; i<num_of_pcs; i++) { if(std::get<2>(pc_vec[i])==0) pc_vec.erase(pc_vec.begin()+i); } //second try //No exception here but the dynamic size of the vector again won't …

Member Avatar
Member Avatar
+0 forum 6

Hello! I'm writing some mathematical iterators over some big classes that I don't want to store in memory at all, but I still want to be able to iterate through them. As an example (but I have a few of these), think of a class `Combinations({0,1,2...7}, 3)`, which means I would like to iterate over all subsets of size 3 from the set `{0,1,2...7}` I already have the code for this, that's not my problem. The thing is, I'd like to make it compatible with the STL. Right now, I have only one class, which is called "CombinationsIterator" that has …

Member Avatar
Member Avatar
+1 forum 2

I was doing some exercise from C++ Primer and one of them required us to copy a set of value from array into a vector container. Then, we are suppose to remove all the even numbers from the vector container. Hoever, when I tried to display them out by incrementing the iterator instead of using a normal for loop, I actually had obtained additional garbage values. Here is my code: #include <vector> #include <list> #include <iostream> int main() { std::vector<int>vec; std::list<int>li; int ia[]={0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89}; for(int i(0); i<sizeof(ia);++i) { vec.push_back(ia[i]); li.push_back(ia[i]); } …

Member Avatar
Member Avatar
+0 forum 4

Alright so I've been struggling to find the error but I simply can't. Given the following code: #include <iostream> #include <vector> enum ScriptType { SCRIPT_TYPE_CREATURE_SCRIPT, SCRIPT_TYPE_PLAYER_SCRIPT }; class Script { private: ScriptType type; public: Script(ScriptType type) : type(type) { } ScriptType getScriptType() const { return type; } }; class CreatureScript : public Script { public: CreatureScript() : Script(SCRIPT_TYPE_CREATURE_SCRIPT) { } virtual void OnDealDamage() { } virtual void OnKill() { } }; class PlayerScript : public Script { public: PlayerScript() : Script(SCRIPT_TYPE_PLAYER_SCRIPT) { } virtual void OnSay() { } }; class ScriptFactory { private: static std::vector<Script *> scripts; public: static void …

Member Avatar
Member Avatar
+0 forum 4

Hey guys, I've tried writing code to reverse my sentence, but I keep getting an error in my for loop saying "Expected Expression". Also, should the asterisk go after the "modify" in my code? I'm not sure how pointers are exactly supposed to work and I haven't been given any errors for doing so yet. Thanks in advance, and here is my code! void f1 (const char* sentence) { char sentence2; sentence = &sentence2; char* modify = &sentence2; basic_string <char>::reverse_iterator revSentence; cout << "The reversed string is:" << endl; for (revSentence = modify*.rbegin(); revSentence !=modify*.rend(); modify*++) { cout << *revSentence; …

Member Avatar
Member Avatar
+0 forum 5

I am trying to input an int into the end of a singly linked list. I have already created the code to put a number at the end of the list but when I go to print it out, it only prints out (in my test class) the int that will go at the end of the list rather than printing out the whole list(and then . mylist.addEnd(8); System.out.println("after addEnd"); anIterator = mylist.iterator(); // reads mylist collection for (int i=0;i<mylist.getSize();i++) System.out.print(anIterator.next() + " "); If this seems correct then something must be wrong with my addEnd method.

Member Avatar
Member Avatar
+0 forum 3

Dear all, I have stuck with one problem. I have taken one Hashmap, Performing some input operation,remove operation on it. So after performing remove operation, i want value of 2 successive key in two different array. Also i want to match 1st key's value with all other keys' value. Like 1st key value matched with 2nd kay value,then 3rd key value upto last key Again 2nd key value matched with 3rd kay value,then 4th key value upto last key. I ahve also make some code but it's not working properly. Here "m1" is my hash map.Originally there were 20 key …

Member Avatar
Member Avatar
-1 forum 5

I'm trying to compare the element pointed by an iterator to an integer, but so far i haven't had any luck in doing so. please correct whatever i'm doing wrong. Thank you. vector<int>::iterator y; for (int i = 0;i<n2;i++) { cin>>temp; cnt = 0; y = lower_bound(a.begin(),a.end(),temp); if (*y == temp) cout <<temp<<"found at"<<int(y-a.begin()+1)<<endl; else cout<<temp<<"Not found"<<endl; } error msg = "vector iterator not dereferencable"

Member Avatar
Member Avatar
+0 forum 4

Hi all, I actually need to use a sorted map.So i tried using Tree Map.But the problem is that This works well with sort key having values upto 9. With more than 10 items I see abrupt results with sort key arranged in the order = 1, 10, 11,12,--------17, 2, 3, 4, 5, 6, 7, 8, 9. I even tried using Comparator for this ....But the same way it is sorted...How to make it arrange in the correct order of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11? Please find my code below public static TreeMap sortByComparator(TreeMap …

Member Avatar
Member Avatar
+0 forum 2

So my java class is a joke, and we're expected to learn how to do our assignments via the web...I'm a hard worker, but I'm just not good at this. [Assignment pic](http://oi50.tinypic.com/aw3zx2.jpg) There's the assignment image. I think i got how to do the first part with the adding random numbers to the list and sorting...but I can't find other resources online that help me understand the rest. Here's my code right now import java.util.*; import java.io.IOException; import java.util.Scanner; import java.util.Random; public class LinkedListProgram { public static void main(String[] args) throws IOException { int i,number, ran; Scanner sc = new …

Member Avatar
Member Avatar
+0 forum 7

Hey bros. I made a program that prints out random numbers in a linked list. I want to have the index number next to each of the random numbers in the list. I guess for example like: 1 25 2 123 3 56 ect. Here's m program so far, and it only sorts and prints out the list, but doesn't have an index next to each random integer. I think you use a list iterator to do this, but I can't figure it out... my output right now is like [23 58 129] but I kinda of want it to …

Member Avatar
Member Avatar
+0 forum 1

Hola code gurus, I’m wondering if there’s a way to use a string to access a specific item in a matrix of int[X]. I have a program which uses enums as iterators to reference a large amount of data. To select an item in the matrix, the user will enter a string, which is also an enum, which also must serve as an iterator for something in the matrix. Here is a toybox example: #include <iostream> #include <string> using namespace std; enum NyNumbers { First, Second, Third, Forth, LAST_VALUE }; int main(int argc, char* argv[]) { int Matrix[LAST_VALUE] = { …

Member Avatar
Member Avatar
+0 forum 3

Hi all, I am doing something like this int f(){ umap::iterator p = mmap.find(s); if(p != mmap.end()){ ....... } } My memory usage is somehow constantly increasing. While exiting the function do I need explicitly delete the iterator p? Thanks in advance, _alen

Member Avatar
Member Avatar
+0 forum 3

Hi, all - I'm relatively new to iterators, and to template functions, so I suppose it was inevitable that the first time I tried to combine them, I'd run into a problem. Here's the function: [CODE]template <class T> int getVectort(ifstream & s, long nbrCells, typename vector<T>::iterator iter) { int rc = 0; int32_t temp; for (int i = 0; i < nbrCells; i++) { s >> hex >> temp; *iter++ = temp; } if (s.fail()) rc = 1; return rc; } [/CODE] It builds OK, but when I attempt to call it, like this: [CODE] rc = getVectort<int32_t>(fileIn, DEMOD_NBR_INPUTS, iter); …

Member Avatar
Member Avatar
+0 forum 5

Hi Everyone, I'm new here and also new in using Java. I want to make an iterator without using the default methods hasNext, hasPrevious. Here is what I've already done using the default methods: [CODE]import java.util.*; public class HomeIterator{ public static void main(String[] args) { int i = 0; ArrayList list_a = new ArrayList(); list_a.add("bicycle"); list_a.add("trycycle"); list_a.add("quadrcycle"); list_a.add("pentacycle"); list_a.add("hexacycle"); ListIterator it = list_a.listIterator(); while (it.hasNext()){ i++; Object element_list_a = it.next(); System.out.println("Element no"+" "+i+":"+" "+element_list_a); } System.out.println(); i++; while (it.hasPrevious()){ i--; Object element_lista_inv = it.previous(); System.out.println("Element no"+" "+i+":"+" "+element_lista_inv); } } } [/CODE] So, I would like to do something similar …

Member Avatar
Member Avatar
+0 forum 2

I am having hard time understanding what the problem is with my code here. Basically I have a map that contains Key objects as the key, and Block* as the value. The point of this for loop is to print out the values that the key contains on to the console in the form of a game board. However it is only printing one role, in a infinite loop. the variable r never gets incremented for some reason. if you guys could help that would be great. Thanks in advance! for (int r=0; r<rows; r++){ for (int c =0; c<columns; …

Member Avatar
Member Avatar
+0 forum 2

I am trying to run what i thought was a pretty basic program to solve a problem concerning strings (besides the point). It turns out that my algorithm works properly but i am stuck on a detail; the following segment of code is the problem: [CODE] int cases; string line; vector<string> frags, results; cin >> cases; while ( cases > 0 ) { while (cin >> line){ frags.push_back(line); line = ""; } results.push_back( find_original(frags) ); //find_original does not modify frags, and returns a string frags.clear(); cases--; }[/CODE] the problem occurs when i input a value of 'cases' that is higher …

Member Avatar
Member Avatar
+0 forum 4

Hi, I have a list of base types which stores multiple derived types. The following code shows a simplified setup. [CODE]#include "Base.h" #include "DerivedA.h" #include "DerivedB.h" #include <iostream> #include <list> void main() { std::list<Base*> base; std::list<Base*> *basePointer = &base; for (int i = 0; i < 10; i++) { basePointer->push_back(new DerivedA); basePointer->push_back(new DerivedB); } }[/CODE] I iterate through this list using the iterator below: [CODE] std::list<Base*>::iterator baseItt = basePointer->begin(); baseItt++; [/CODE] Is there a way I can find out which derived type 'baseItt' is pointing to? I need this to help me with saving and loading in my program. Please …

Member Avatar
Member Avatar
+0 forum 7

I just started looking at iterators in my C++ class and don't think I fully understand enough about them to actually create one. I need to add an iterator to this doubly linked list program: [CODE]#ifndef CSLIST_H #define CSLIST_H #include <iostream> #include <cstdlib> using namespace std; template <class T> struct node // Create a structured node object { T value; // value the node stores node<T>* next; // pointer to the next node node<T>* prev; // pointer to the previous node }; template <class T> class cslist { public: cslist(); // default constructor int size() const; // return the number …

Member Avatar
Member Avatar
+0 forum 2

Hey, it's me again. Again with a topic that has been covered here but reading through the old threads didnt help me to solve my problem. And I am pretty clueless at the moment. Consider the class passenger_queue with 2 functions, namely: [CODE]int passenger_queue::get_passengers_waiting() const { // Insert your code here. Use the iterator defined in this class. int sum = 0; } return sum; } [/CODE]and [CODE]std::vector<passenger*>& passenger_queue::passengers_at_floor(int floor_) { return _waiting_passengers[floor_]; }[/CODE] member variables are: [CODE] typedef std::map<int, std::vector<passenger*> > p_map; p_map _waiting_passengers; std::vector<passenger> _passengers;[/CODE] First of all the comment in the get_passengers_waiting. Apart from the constructor which …

Member Avatar
Member Avatar
+0 forum 11

The End.