Member Avatar

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
0
24
Member Avatar

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
0
17
Member Avatar

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

Member Avatar
0
28
Member Avatar

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
0
26
Member Avatar

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

Member Avatar
0
26
Member Avatar

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
0
22
Member Avatar

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
0
22
Member Avatar

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
0
7
Member Avatar

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
-1
6
Member Avatar

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
0
10
Member Avatar

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
0
4
Member Avatar

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
0
3
Member Avatar

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
0
5
Member Avatar

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
0
8
Member Avatar

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
0
12
Member Avatar

Hey all, I'm implementing a hash table for a spell checker and need some assistance on vector const_iterator - at least I think that's where the problem lies. Can anyone assist in diagnosing how to fix these compiler errors? I initially got this error, which i think will come back once I fix the is_present function: main.cpp:101: undefined reference to `ns_1::table<word_record>::is_present(int) const' I am now getting the below errors and can't figure out if I have a data type issue or am just defining the const_iterator incorrectly or ... Note that line 67 in the errors corresponds to line 11 …

Member Avatar
0
10

The End.