C++ :: How To Search The Loop Of Queues Of Vector
Feb 14, 2013
i have a paradigm where a integer before gets enqueued to a queue, the loop of queues in a vector is searched and integer is enqueued to a queue which has minimum size among the queues. the following code shows the operation.
#include <vector>
#include <queue>
std::vector<std::queue<int> > q
int min_index = 0;
std::size_t size = q.size();
for( i=0; i<size; i++){ //accessing loop of queues
if(q[min_index].size() > q[i].size())
min_index = i; // Now q[min_index] is the shortest queue}
q[min_index].push(int)
next i am trying to extend my paradigm with that the condition the integers should be enqueued to the shortest queue until it becomes maximum size among the queues.
do{
q[min_index].push(int)
} while(q[min_index].size > queue sizes in the vector loop except this queue )
how to search the loop of queues of vector in the while ()
View 13 Replies
ADVERTISEMENT
Jan 20, 2013
I have the following code which will find the minimum size queue among a vector of queues and the minimimum size queue will enqueue(push) the int
std::vector<std::queue<int> > q
void enqueue(){
int min_index = 1;
std::size_t size = q.size();
for( i=2; i<size; i++) //accessing loop of queues
if(q[min_index].size() > q[i].size())
min_index = i; // Now q[min_index] is the shortest queue
q[min_index].push(int)
}
Now my another paradigm is to do the dequeue(pop) operation in another function(shown below), bt i need to access all vector of queues declared in enqueue() function. how can i access the loop of queues given in the enqueue() function?
void dequeue(){
//q.pop operation , access all the queues in the loop of queues
}
willq[i].pop(int);
Access all the queues in the enqueue function and does the pop operation?
View 1 Replies
View Related
Feb 20, 2013
I have a paradigm in a loop of queues of a vector,if a condition is true,increase sizes of the queue of that particular queue in the loop of queues, if condition is false, the queuesize is left as such in loop of queues. After this operation i need to search the queue sizes of all queues and enqueue in the shortest queue.
I want to do something like the code given below
#include <vector>
#include <queue>
int min_index = 0;
std::vector<std::queue<int> > q
std::size_t size = q.size();
[Code] ....
How to implement this logic?
will q[i].size=q[i].size+5 increase the queuesize by 5 for the ith queue?
View 12 Replies
View Related
Apr 23, 2012
Just doing some work where i need to produce a database in C++ for DVD now I have done most of it but I'm stuck on some bits. I have split the database up into different files but I will post the files which are important. How to do a search function. I got told it's called "Bubble search" and then a delete function which i think is called "Vector delete".
My header file
Code:
#ifndef DVD_DB_H
#define DVD_DB_H
#include "dvd.h"
#include <vector>
class dvdDB {
private:
std::vector<DVD> dvds; // A container that contains an arry of DVDs
[Code] .....
View 1 Replies
View Related
Nov 22, 2013
How could I search a vector element with multiple strings
i.e. vector.at(i) = This is a test
How could I search that to look for the word "test" in that element?
View 7 Replies
View Related
Sep 25, 2012
I'm trying to do a binary search on a vector of strings and keep getting this error. This is the PhoneEntry header file with the class declaration:
Code:
using namespace std;
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
#include"phoneNumber.h"
[Code] .....
View 5 Replies
View Related
Oct 13, 2014
I am creating a binary search program that lets the user input up to 10 integers and then the program displays the array and sorts it. When the user is prompted to select an integer, if the integer is in the array, the program responds with the array subscript part. I can get the loop to work once and maximum twice, but then it wont search for the array or say value not found even though the number in in the array. I tried making the values NULL but that only lets me go through it one more time.
Here is my code:
#include <iostream>
using namespace std;
//Functions
void printArray(int);
void selection(int);
int binarySearch(int,int,int);
[code]....
View 6 Replies
View Related
Sep 30, 2013
How would you search in a vector array from a user input string?
ex: user input : "Hello"
output: search vector array and find the line that has the string "Hello" and output the array "Hello" is on?
View 1 Replies
View Related
Jun 3, 2013
For my homework I need to create a hash table with a size of 7. I also need to create a queue that holds 3 names maximum for each table position
View 1 Replies
View Related
Feb 25, 2014
I'm trying to solve the ferry loading problem using two queues. My problem is I can't enter in data after the first set is scanned in, I'm assuming there is a problem with my loop, such that the scan function doesn't get called after one iteration. In the example I marked the data I can't enter. An example correct input would be:
correct input:
1 - can enter data20 4 - can enter data
380 left - can enter data
720 left - can't enter data
1340 right - can't enter data
1040 left - can't enter data
correct output: 3
my incorrect output: 1
Code:
#include <stdio.h>
#include <stdlib.h>
#include "my_linked_list.h"
#include "my_linked_list.c"
#include "status.h"
[Code] ....
View 6 Replies
View Related
Feb 22, 2013
I'm having troubles with this program I made. Put it simply, it's a basic program using Queues. Whenever I try to input a value, my compiler(Dev C) suddenly stops working. The .exe file crashes and I've no way on how I can execute my program.
Code :
#include<iostream>
using namespace std;
int *queue;
int rear, front, queueSize;
void enqueue();
void dequeue();
void display();
[Code] ....
View 1 Replies
View Related
Mar 22, 2013
For a beginners C++ lab, I have a base class Employee and two derived classes HourlyEmployee and SalaryEmployee. In main, I have a vector defined as vector <Employee *> VEmp; It's then passed to a function to get the input, which works fine. But what I'm struggling with is another function with the header "printList(const vector <Employee *> & Ve)". It's supposed to loop through the vector and call the appropriate printPay function, which is a seperate print function inside each derived class. How do I loop through the vector and print it out? I was trying to do a for loop and something like "Ve[i].printPay();", but that doesn't work. So how would I do it?
Here's some snippets of the relevant code.
class Employee {
....
virtual void printPay() = 0;
};
class HourlyEmployee : public Employee {
[Code] ....
View 4 Replies
View Related
Jul 21, 2013
Ok my assignment has me doing vector math with some canned code provided for me by the instructor This is the header file to the class I'm working with and the .cpp file as far as I've gotten it.
#pragma once
#include "Scalar.h"
class Vector2D {
public:
Vector2D();
Vector2D( const Vector2D& ) ;// copy constructor
Vector2D( Scalar element[2] ) ; // initialize with an array
[Code] ....
I'm having trouble seeing which data members I'm multiplying together and what the initial state, continuing state, and after loop action I'm supposed to be using in the for loop.
View 1 Replies
View Related
Jul 12, 2014
How would I make a loop that would simultaneously compute both the max and min of a vector?
View 2 Replies
View Related
Feb 6, 2014
In this program what i'm doing is to search for a number when the vector is in order, and count how many times that number appears, the problem is that count how many times the number does not appear, but when its appear the program stays in an "standby mode" and the cursor does not move.
int buscarNumOrdenado (int x [MAX]) //MAX is the define {
int num,d, LimiteInferior, LimiteSuperior,mitad;
d=0;
LimiteInferior = 0;
LimiteSuperior = MAX-1;
[Code] ....
View 2 Replies
View Related
Apr 18, 2013
I am having a problem with a program. I have a bunch of classes all derived from the same base class. I want to loop through a vector of objects, calling a function in each. The problem is that it doesn't matter which class the objects are, only the function defined in the base class is called.
I simplified the code as far as possible to replicate the problem. As you see, I would like a mix of numbers 1,2,3 as the output, however using the vector the only number output is 1. Here is a copy of the output by the way:
base->num() : 1
a->num() : 2
b->num() : 3
(*it)->num() : 1
(*it)->num() : 1
(*it)->num() : 1
I suspect this is the "slice" problem, because the vector is defined with pointers to the base class so it uses the base class functions? The question is how to get around it? How can I loop through a vector of objects sharing the same base class but calling each by their correct member functions?
Code:
#include <iostream>
#include <vector>
class Base {
public:
int num() { return 1;}
[Code] .....
View 5 Replies
View Related
Nov 21, 2014
My loop is outputting data incorrectly. I have inbound web data that come in sets of 7. I am trying to in insert the 7 records into a vector and then display the vector content followed by a new line.
Code:
char buff[BUFSIZ];
FILE *fp = stdout;
char * cstr = buff;
vector<std::string> vrecords;
while(std::fgets(buff, sizeof buff, fp) != NULL){
for(int i = 0; i < 6; ++i){
[Code] ....
expected output:
Found buy!
198397652
2014-11-14 15:10:10
Buy
0.00517290
0.00100000
0.00100000
0.00000517
[Code] ....
View 14 Replies
View Related
Feb 24, 2014
The code is supposed to take either an int or a string (and their respective vectors) and insert a given int or string into the vector in ascending order. My code works properly for ints, but it's having a problem with strings.
The order I get with the strings given is
penguin
banana
great
jungle
For some reason comparing penguin to banana/great doesn't give the expected result. The template attached only includes the function and the private vectors needed for the function.
template<class T>
class orderedList {
public:
void insert(const T& item);
private:
vector<T> list;
int total = 0;
[Code] ....
View 11 Replies
View Related
Jan 20, 2014
So I have this problem with not being able to iterate over a vector in a nested for-loop. Here's the nested for-loop:
bool innerHit = false;
for (std::vector<Sprite*>::iterator outerIter = sprites.begin(); outerIter != sprites.end() && (!sprites.empty()); outerIter++) {
Sprite* spriteOne = *outerIter;
for (std::vector<Sprite*>::reverse_iterator innerIter = sprites.rbegin(); innerIter != sprites.rend() && (!sprites.empty()); innerIter++) {
Sprite* spriteTwo = *innerIter;
[Code] .....
What happens is, after having called the collisionDestroy-function and the program tries to execute the nest loop in the outer for-loop, it all crashes with the text "Expression: vector iterator not decrementable", which I understand is because the iterator will have already become useless. The question is: know this, how do I fix it? I can't seem to get a hang of it.
Here's the collisionDestroy-function (the collisionReaction does nothing but sets a few local variables):
void Enemy::collisionDestroy(std::vector<Sprite*>& sprites) {
for (std::vector<Sprite*>::iterator iter = sprites.begin(); iter != sprites.end(); iter++) {
Enemy* tmp = dynamic_cast<Enemy*>(*iter);
if (this == tmp && collisionType == 3 || collisionType == 1) {
sprites.erase(iter);
break;
}
}
}
View 14 Replies
View Related
Mar 7, 2013
I am trying to implement a Task scheduler where i have n number of tasks. The Idea behind my task scheduler is that in a loop of queues of a vector, task should get enqueued to the shortest queue among the loop of queues, which is done by the following code.
#include <vector>
#include <queue>
std::vector<std::queue<int> > q
int min_index = 0;
task t // implemented in the other part of the program
[Code] ....
Next i am trying to extend this paradigm to reduce the overhead time of the scheduler, Instead of searching the shortest queue every time, search after some condition ie. search the shortest queue after 5 tasks gets enqueued to the shortest queue.
i need to do something like this
#include <vector>
#include <queue>
std::vector<std::queue<int> > q
task t // implemented in the other part of the program
while(q[min_index].size()!=q[min_index].size()+5) // check whether current min_index queue's size is increased 5 more times if not goto enqueue
[code].....
View 1 Replies
View Related
Sep 16, 2013
Write a program to find the number of comparisons using the binary search and sequential search algorithms
//main.cpp
#include <cstdlib>
#include <iostream>
#include "orderedArrayListType.h"
using namespace std;
int main() {
cout << "." << endl;
[code]....
View 4 Replies
View Related
Oct 7, 2014
inputting a search array. I tried putting a binary search but I can't get it to work. everything else works up until I put the value I am searching for in the array, then it just crashes.
How it suppose to work: input 2 coordinates with a value each then it calculates the distance between them then it suppose to let user search the coordinates for a value and state if found which coordinate it is at.
#include "stdafx.h"
#include <iostream>
#include <iomanip> //for setprecision
#include <math.h>
[Code].....
View 3 Replies
View Related
Feb 9, 2015
How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.
here is my code:
#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)
[Code].....
View 3 Replies
View Related
Jun 7, 2012
I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,
N,N'-dimethylethylenediamine.mol
This is the function that opens the file :
Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );
[Code] ....
The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.
View 9 Replies
View Related
May 13, 2013
I need to create a class vector as a template and define operations on vectors.
And this is what I made.
#include<iostream>
using namespace std;
template<class T>
[Code].....
View 2 Replies
View Related
Mar 26, 2013
Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>
How do I make a function that creates vector of vector of every different integers?
<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>
and so on...
View 2 Replies
View Related