C++ :: Pair Function To Use 3 Elements?
Feb 14, 2012is it possible to alter the Pair function to make it use 3 elements?
View 13 Repliesis it possible to alter the Pair function to make it use 3 elements?
View 13 RepliesI want to use a dataset of type set which will have the type pair<char,string> or pair<string,string>. How can i insert values into the set, because i have to initialize the set and will not change the set during the program.
View 3 Replies View Relatedhere is a piece of my code:
while(T--)
{
std::vector<std::pair<int, int> > *dragon;
std::vector<std::pair<int, int> > *villager;
[Code]....
I now wish to print the elements of the vector. How do I do it?
What is the correct syntax? I tried something like
Code:
class myclass {
multimap<int, void(*)()> mm_fn;
...
void afunction();
...
void anotherfunction(int anint){
mm_fn.insert(pair<int,void(*)()>(anint,afunction));
}
}
and the compiler does not like it.
int main()
{
int a=0, c, d, N, K;
bool stopBool = 0;
[Code]....
This is supposed to find take a number N and K and find all numbers between 0 and N that equal K and cout the number of pairs that fit it but it doesn't work.
I have tried to implement a much simplified version of boost::compressed_pair.What follows is a partially specialized EBO_pair<T1, T2> class template, written in C++11.The first type T1 is constrained to not be empty.The second type T2 may or may not be empty.
#pragma once
#include <memory>
#include <type_traits>
#include <utility>
namespace dsa
}
[code]...
Edit: added non-member swap() function template.
Goal: To allocate some memory as a char*, read in some binary data, re-interpret it as a float* and then free the memory.
My code looks like:
void someFunction(float* &result) {
char * tmp = new char[1000];
//...Fill the char buffer here...
result = (float*)tmp; //Reinterpret binary data as floats
[Code] ....
Is the cast back to char* necessary on the red line (or could I have validly left it as float*)? Would it be different if I had written char * tmp = (char*)malloc(sizeof(char)*1000); on the blue line (and correspondingly used free (char*)floatData on the red line?
I have an assigment to make program which deletes from sentence all words with character count which is equal to pair number , for example - [ I like C ] and the result of this program should be [I C] because the word like contains 4 characters which is pair and it should be removed.
So I started writing my program and I am stuck at this block of code -
#include <stdio.h>
#include <stdlib.h>
main () {
char text[100], blank[100];
int c=0,d=0,i,j;
gets(text);
[Code] ....
To explain what is happening - I go through all string and search for first ' ' space symbol and check its value. If it is pair then my program prints that it is not pair[because last character before space had not pair number of characters], but the hardest part comes in when i have two not pair words , because space takes one character and now when i check if i%2 == 1 the answer is false [0] for the second word .
Code:
const MenuData breakfast[NUMOFBREAKFASTITEMS] = {
{"Egg Breakfast", "Two eggs with a side of bacon and two slices of toast.", "", "2 Eggs, 2 Toats, 2 Bacons", "", 250.00},
{"Pancake Breakfast", "Three Buttermilk pancakes served with butter and syrup.", "", "Three Pancakes, Butter, Syrup", "", 200.00},
[Code]....
What I'm trying to do is call the printReceipt function in the main, but I'm doing it wrong. Here is my attempt.
Code:
printReceipt (const MenuData menu[], qty, info)
I've tried it many other ways but I keep getting an error. I tried researching it on the internet, but I don't get much info.
I have a function where i declared the number of elements in a vector alocated dynamically which returns the vector to the main function. The problem is how can i find the number of elements in main function? I tried length = sizeof(a) / sizeof(int) which gives me the same value, the value of the first element. Here's the code:
int* functie2 (void)
{
int* p;
int c,i;
printf("number of elements: ");
scanf("%d",&c);
[Code]...
I know i could first read the value of c in main and then pass it thorugh parameter, but how can i do it the other way arround?I could also send the value of c allocating one more int value to the vector, but i don't want doing so.
Where's the edit button?
"Write a declaration for a function that takes two int parameters and returns an int, and declare a vector whose elements have this function pointer type."
View 9 Replies View RelatedI have a matrix that contains zero and nonzero elements. I want to do a function that return 3 arrays.
The first one is for nonzero elements the second array contains the corresponding row numbers of each nonzero element the third array contains the corresponding column numbers of each nonzero element.
I'm writing a program that will implement BubbleSort and MergeSort and time them as they sort a dynamic array with N elements. These are the instructions on what my main.cpp file should do.
main.cpp
Include all needed libraries for timing and random number generation while the number of element, N, is less than 100001 repeat the following.
create an array with N (initially 10) random elements
sort the same array with Bubble and Merge sort
time how long it takes each algorithm in microseconds (see example below)
Increase N by a factor of 10 (N *= 10)
Hint: you may want to put your merge sort object on the heap and delete it with every iteration of this loop
And this is what I have so far for my main.cpp
#include <iostream>
#include <sys/time.h>
#include <ctime>
#include <cstdlib>
#include <stack>
#include <iomanip>
#include "BubbleSort.h"
//#include "MergeSort.h"
[Code] .....
One of the errors that I'm running into is that I'm not sure how to correctly call the function I think?
I'm working on a piece of code written long time ago. Without getting in the details or too much context here, there is a function that declares an array of char of a size of 350,000 elements, in order to fill it (using a pointer) with the list of all running processes on the machine (using "ps -ejf" on a Linux box).
The size of the char array has been changed from 40,000 to 350,000 sometime along the years, probably because of a lack of space required.
What kind on data structure / storage would you use to store the running processes in order to eventually search for a value in it?
I have to write a function called sortMe that sorts the elements of an array in numerical order from highest to lowest values (descending order) or vice versa (ascending order).
The assignment asks to: NOT re-arrange elements in the array; instead, it uses a second array, an array of indexes for the elements in the original array and then sortMe sorts the second array based on the values in the original array. A sorted version of the original array can then be produced with these sorted indexes.
Header of the function sortMe must be as shown below:
void sortMe(int array[],int sortedIndexes [], int size, char mode)
When mode is 'a', the function sorts the array in the ascending order, and when mode is 'd', the function sorts it in the descending order.
Declare and initialize the array array.
Declare the array sortedIndexes but do not initialize it. You are going to play with the array sortedIndexes in the function sortMe.
EXAMPLE:
int array[5]={3, 5,-1,10,0};
int sortedIndexes[5];
sortMe(array,sortedIndexes, 5, 'a');
After the function call, the elements of the array sortedIndexes should be: 2,4,0,1,3.
notice that the function does not e-arrange the elements in the array.
Code:
#include <iostream>
using namespace std;
void sortMe(int[], int, char);
void main() {
int arr[6] = { 14, -5, 5, 0, 22, -99 };
[code]...
how to use the other array.
Find all the prime numbers between a given pair of numbers. Numbers should be read in from an input file called "numbers.txt" and find all the prime numbers between them. Store the prime numbers in an array, then sort the array from greatest to least. Display the array before and after the sort.
I'm stuck on how to put the prime numbers into an array.
The input file has the numbers 1 & 100.
Here's what I have so far.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
fin.open("numbers.txt");
[Code] .....
Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.
View 6 Replies View Relatedwrite a swap function to swap 2 elements in the vector?
View 3 Replies View RelatedI'm trying to get unique elements from a set but it prints for each item all the rest, for example if i have 3 items in a set it will print 6 instead of 3. I use this code:
for (set<string>::iterator i = mails.begin(); i != mails.end(); ++i) {
cout << *i << endl;
}
Is there any way to print just the unique items from a set ?
The set contains:
1
2
3
But it will print
1
2
3
2
3
3
int sub(string &temp,int begin,int end){
if(end-begin == 0){
return temp[begin];
} else {
return sub(temp,begin+1,end) + temp[begin];}
}
I want to add all subset elements, but when i do
cout << sub(5) << endl;
it print 150....
I've a file that looks like this:
84484-37.96-Castor, Kathy
39050-69.68-Chandler, Ben
26183-70.84-Costello, Jerry
I have successfully read each element the id, grade and name into 3 separate array. Now i need to add a new student with an id and grade
How do i do this?
This is what I have.
int addStudent( int Iarray[], double dArray[], string sArray[], int newID,
double newGrade, string newName, int size ) {
char ready;
int index;
cout << endl;
cout << "Enter new student ID number : ";
[Code]...
I have a question regarding the elements of an array. Suppose I have a 3 by 3 string array (char x[3][4] ) , and I initialize all the elements to x's , the array would then look like this :
xxx
xxx
xxx
I'm curious if there will be a value if I try to access and element outside the array. As I have to write a code to determine if I have reached the end of an array. The only way I can think of is to border the entire array with o's , making it look like this :
ooooo
oxxxo
oxxxo
oxxxo
ooooo
Is there any other way to do this?
As far as I'm understand we have only access to .h files of elements of STL. So other words in MS VC 2010 we haven't opportunity to see exact implementation of elements of STL. Where I can find implementation of STL elements or substitute algorithm that do the same thinks like stack from stl
View 2 Replies View RelatedI'm writing a program with a class containing a private std::vector<bool>. I chose bool because the vector represents a 2D array (think grid) and I only need 2 states per cell. I kept it one-dimensional as this hardly complicates things.
My problem is that I don't know how to initialize the vector, i.e. fill it with 0's.
The grid's resolution is not known at compile time, so I imagine I have to set the size (and content) of the vector in the class constructor.
Here's what I have tried among several things:
Code: World::World(const u_short worldsize)
{
grid.reserve(worldsize * worldsize); // grid is the private vector; square dimensions.
std::fill(grid.begin(), grid.end(), 0);
std::cout << grid.size();
} The output is 0. Only std::vector::push_back seems to have an effect on size(), but judging by its description, it doesn't look like the right candidate to populate a vector with zeros. Correct me if I'm wrong.
Frankly I expected line 3 to set the vector's size.
Trying to concatenate the 1st and 2nd element from an array of chars. Then this to be used as a hex value.
so for example
specs[0]='f'
specs[1]='4'
concatenate_value=0xf4
Write the definition of a function reverse , whose first parameter is an array of integers and whose second parameter is the number of elements in thearray . The function reverses the elements of the array . The function does not return a value .
Code:
void reverse(int a[], int num) {
for ( int i=0; i <= num/2 ; i++){
int temp = a[i];
a[i] = a[num-i-1];
a[num-i-1] = temp;
} }
This is supposed to be the answer but I'm not quite sure why this is. I understand everything up until the actual loop. For one, shouldn't "int i" be declared outside the loop (I thought perhaps this was an error in the solutions)?
The main thing that I do not understand is the conditional statement.
Code: i<=num/2;
I don't understand why the "num/2" is necessary here. Also I can't really remember but is there a command that actually reverses an array?