C++ :: Recursive Function 2 - Create Vector Of Vector Of Integers

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


ADVERTISEMENT

C++ :: Program To Create A Vector Of Integers

Feb 10, 2015

1. Write a c++ program to create a vector of integers. copy the vector contents into list, sort the contents, then copy selected items into another vector(like elements less than 10 etc)

View 1 Replies View Related

C++ :: Recursive Function - Creating Vector Of Every Unique Combination Of Commands

Mar 24, 2013

I'm trying to write a recursive function that takes in a vector of strings that contains

"1 forward", "2 forward", "rotate left", "2 backwards" ... etc

how can I write recursive function that creates a vector of every unique combination of commands?

View 8 Replies View Related

C++ :: Create Class Vector As Template And Define Operations On Vector?

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

C++ :: Using Vector Push Back Function To Output Contents Of Vector (similar To Array)

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

C++ :: Find Function To A Vector Of Structures Vector

Jul 5, 2013

I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.

For instance, here is a test program that successfully finds the maximum according to just one member of the structure:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

[Code] ....

And the output was this, as expected:
Maximum element S.a of vector<S> vec is at: 9
[I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6
[I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7
[I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8
[I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9

However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.

This was the description of the std::find function that I found: find - C++ Reference

Code:
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);

I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference

Code:
template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:

Code:
int x=7;
std::vector<S>::iterator result;
result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;

Is there another std function that I can use to make search and find an element according to just one member of myStruct?

Or perhaps there is a clever way to pass two arguments to the unary predicate.

View 4 Replies View Related

C++ :: Vector With Strings And Integers

Apr 5, 2013

I have a file where the first column shows letters, second column shows numbers.

How could I make it to print everything exactly how it is in the file – a vector/array with strings and integers?

View 13 Replies View Related

C++ :: How To Make 5x3 2D Vector Of Integers

Feb 8, 2014

I am trying to make a 5x3 2D-vector of integers, then set its i-capacity to be 5 and j-capacity to be 3, i.e:

vec2D[i][j] i = 1,2,3,4,5 j = 1,2,3

and then assign integer values to it.

#include <vector>
#include <iostream>
using namespace std;
int main () {
vector<vector<int> > vec2D;

[Code] ....

It compiles, but does not work properly:

Test.exe exited with code -1073741819

i-capacity before reserve: 0
i-capacity after reserve: 5

i = 0
j-capacity before reserve: 336043326
j-capacity after reserve: 336043326
i = 1
j-capacity before reserve: 4282929217
j-capacity after reserve: 4282929217
Press <RETURN> to close this window...

I am trying to convert a C code with dynamic 2D arrays, to a C++ code. I prefer to keep the vec2D[i][j] = ... way of assignment instead of using vec2D.push_back(...).

View 8 Replies View Related

C++ :: Vector Of Integers - How To Print Indicators

Apr 8, 2014

I have a vector of integers, which can take values 0 or 1, say

0 0 0 1 1

I need to print indicators, telling in which position 1-values occur. So

0 0 0 1 0
and
0 0 0 0 1

View 11 Replies View Related

C :: Binary Tree - Return A Vector Of Integers

Oct 19, 2014

So I have a Binary Tree and I need to return a vector of integers (the nodes) of the heaviest path in the tree.

First, is it possible to do in C? Because I think a vector is an ADT in C++.

I've started writing something recursive, which worked for a balanced tree of height 1, and failed for longer height.

This is what I've written - [URL] ....

View 6 Replies View Related

C :: Create Function To Create A Dynamic Array That Fill With Randomly Generated Integers From 0 To 50

Oct 26, 2013

I have a struct called Array and I'm to create a function to create a dynamic array that's fill with randomly generated integers from 0 to 50 (inclusive) and a function to destroy the array for freeing its memory. Below the code that I have written so far.

Code:

* Struct */
typedef struct {int *pArray; //the dynamic array
int length; //the size of the dynamic array}Array;
/* Function to create a dynamic array */
Array *initializeArray (int length) {int i;
}

[code]....

View 7 Replies View Related

C++ :: Generate Random Integers In The Range And Store Them In A Vector

Sep 3, 2014

You are to write a C++ program to generate random integers in the range [ LOW = 1, HIGH = 10000 ] and to store them in a vector < int > of size VEC_SIZE = 250. Then, sort the contents of the vector (in ascending order) and display it on stdout.

To sort the contents of a vector, use the sort ( ) function from the STL. In addition to the main ( ) routine, implement the following subroutines in your program:

• void genRndNums ( vector < int >& v ) : This routine generates VEC_SIZE integers and puts them in vector v. Initializes the random number generator (RNG) by calling the function srand ( ) with the seed value SEED = 1, and generates random integers by calling the function rand ( ).

• void printVec ( const vector < int >& v ) : This routine displays the contents of vector v on stdout, printing exactly NO_ITEMS = 12 numbers on a single line, except perhaps the last line. The sorted numbers need to be properly aligned on the output. For each printed number, allocate ITEM_W = 5 spaces on stdout.

Programming Notes:

• You are not allowed to use any I/O functions from the C library, such as scanf or printf. Instead, use the I/O functions from the C++ library, such as cin or cout.
• Let v be a vector of integers, then the call: sort ( v.begin ( ), v.end ( ) ) sorts the elements of v in ascending order. The detailed description of the sort ( ) routine can be found on the course web site and in the course textbook.
• Execute the srand ( ) function only once before generating the first random integer with the given seed value SEED. The rand ( ) function generates a random integer in the range [ 0, RAND_MAX ], where the constant value RAND_MAX is the largest random integer returned by the rand ( ) function and its value is system dependent. To normalize the return value to a value in the range [ LOW, HIGH ], execute: rand ( ) % ( HIGH – LOW + 1 ) + LOW.

View 1 Replies View Related

C++ :: Sorting Large Numbers Of Vector Of Random Integers Returns All Zero

Jul 31, 2014

I tried to sort a large numbers of vector of random integers with std::sort(), but when the number increases over 10M, std::sort returns all zero in values. Does std::sort have a limitation of input numbers?

View 1 Replies View Related

C/C++ :: Statistical Analysis Of Vector Data Set Of Integers - Frequency Distribution?

Feb 16, 2014

I am working on my second c++ project that is a statistical analysis of a vector data set of integers. I created a struct called range with a maximum value, minimum value and count for occurrence. The frequencies are stored in a vector<range>.

I have tried messing around with the increment to get the high range test above the maximum element, though my ranges cumulatively increase by 9.9. I'm not sure how the professor's program has the distance between high and low ranges as 10 instead of 9.9, which is the increment. I'm also unsure why half way through his buckets, the ranges appear as 9.99 briefly and then return to 10 after (40.00 - 49.99 then 49.99 - 59.99). I feel like I am missing something very obvious on line 04 or 10 in my code.

Here is the code for the frequency that accepts a vector reference for my data set of integers.

void frequency(vector<int>& data_set){
double max = findMax(data_set);
double min = findMin(data_set);
double increment = (max - min) / 10;
double percentage = 0.0;

[Code] ....

View 1 Replies View Related

C/C++ :: How To Create A Vector In Header File That CPP Object Can Use

Apr 25, 2015

I'm working on a grocery store inventory project. One part is to have a shopping cart, where customers can put in up to 20 items. Because there can be up to 20 shopping carts at one time, I want to use a vector inside the cart object to represent all the individual food items.

Here's my code,

Header:

#ifndef CART_H
#define CART_H
#include <vector>
class Cart {
public:
Cart();
Cart(std::vector< int >, std::vector< int >)

[Code] ....

View 9 Replies View Related

C++ :: Create A Vector Using A Structure Definition As The Basis?

Jan 25, 2013

I was trying to apply what is here (as someone who writes rarely and has to relearn everything each time): [URL] ....

I'm using a header file to define the structure:

#ifndef EINSTEIN_H
#define EINSTEIN_H
#include <stdio.h>
#include <vector>
struct SizeAll{
double year;
double R;
SizeAll() { year = 0; R = 0; }

[Code] ...

This creates quite a mess. It seems that somehow the "vector" declaration isn't working as the referenced web link seems to suggest that it should. I presume that, as usual, clearing one real error will eliminate the cascade of errors the first real error produces. Why won't VC++ accept the "vector" identifier?

The error messages that follow the build attempt are:

Friedman.cpp
d:documents and settingsxxmy documentsvisual studio 2010projectsfriedmanfriedmanEinstein.h(22):
warning C4996: 'fopen': This function or variable may be unsafe.
Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

[Code] .....

View 14 Replies View Related

C++ :: Open File And Read In Rows To String Vector And Return Vector

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

C/C++ :: Cannot Print Vector Of Vector Of Doubles

Mar 31, 2014

I am trying to print a matrix solution that I've stored in a vector of doubles. The original matrix was stored in a vector of vector of doubles. I want to print to the screen and an output file. Right now it just prints to screen column-style and the output file is horizontal-style. I tried to change the solution from vector of doubles to a vector of vector of doubles like the original matrix but when I run my code it crashes. Here is what I am referring to:

void readMatrix(vector<vector<double>> &matrix, vector<double> &b, int &N, string input) {
ifstream in(input);
in >> N;
matrix.resize(N);
b.resize(N);

[Code] ....

However when I change my printResult function to this (I removed the string argument because I just want to get it working to the screen first):

void printResult(vector<vector<double>> &sol, const int N) {
//ofstream out(output);
//int j;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++){

[Code] ....

The program crashes. Am I even printing the matrix correctly?

View 4 Replies View Related

C/C++ :: Swapping Vector Int And Vector Strings

Mar 30, 2013

I want to have it so that when i ask for the person witch item they want to drop on the ground it goes into another vector that i can pick back up the item if they want it back and erase when they walk away.

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>  
using namespace std;  
    struct InventoryItem

[Code] ....

View 4 Replies View Related

C++ :: Can't Read In Matrix Into Vector Of Vector Of Int

Mar 24, 2012

Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>

[Code]....

Why is it reading in nothing for the arrays, and also making the size of the total thing the total number of numbers? It should have a size of 2, not 5.

View 14 Replies View Related

C++ :: Copy Part Of Vector 1 To Vector 2

Jun 21, 2012

I am trying to copy vector to vector as follow:

Code:

std::vector<unsigned char> vec1;
//insert some values into vec1
std::vector<unsigned char> vec2;

Now I want to to copy 2 bytes from vec1 starting at index 5., why do i need to know how many bytes from the end of vec1?? can't i just specify how many bytes i want to copy from starting index?

std::copy(vec1.begin()+5, vec1.end()-??, vec2.begin());

View 2 Replies View Related

C++ :: Using A Referenced Vector In A Function

Jun 22, 2013

tell me what I am doing wrong here?

Code: void print(const std::vector<int>& vec)
{
for ( std::vector<int>::iterator itr = vec.begin(), end = vec.end(); itr != end; ++itr )
{
std::cout << *itr << std::endl;
}
}

View 4 Replies View Related

C++ :: Passing A Vector To A Function

Nov 3, 2013

i'm trying to send a vector to a function however i am getting these errors

error: invalid initialization of non-const reference of type 'std::vector<sf::Texture>&' from an rvalue of type 'std::vector<sf::Texture> (*)(sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::T|

error: in passing argument 1 of 'int set_explosion_textures(std::vector<sf::Texture>&)'|

my relevant code is

//vector containing fighter explosion frames
std::vector<sf::Texture> f_explosion_textures(sf::Texture f_explosion_1, sf::Texture f_explosion_2, sf::Texture f_explosion_3,

[Code].....

View 7 Replies View Related

C/C++ :: Function Isn't Returning A Vector

Nov 11, 2014

I am writing a function to take two vectors and put them end to end in a third vector. I'm new to working with vectors, and I cannot figure out why my append function is not returning vector C. I had the function print out vector C within it to make sure the logic in the function wasn't the problem, and it worked perfectly. My code is as follows:

#include <iostream>
#include <vector>
using namespace std;
//append function to put vector b after vector a in vector c
vector <int> append(vector <int> a, vector <int> B)/>/>/> {
vector <int> c;

[code]....

and my output is as follows:

Vector A contains: 10 18 123 172
Vector B contains: 283 117 17

The two vectors back to back are:

Obviously, the third vector is not returning from the function to main properly, but why.

View 5 Replies View Related

C++ :: How To Use Function That Returns Vector

Oct 7, 2014

How can I show elements of a vector that is returned by a function in main() ?

Code:
vector<int> merge_sorted(vector<int> a, vector<int> b){
vector<int>temp;
int count = 0;
if(a.size() < b.size()) count = a.size();
else count = b.size();

[Code] .....

View 12 Replies View Related

C++ :: Sorting Function - Vector Swap

Jun 26, 2013

writing a sorting function that has an argument for a vector of ints rather than an array; it should use a selection sort algorithm.Here is what I have:

#include <iostream>
#include <vector>
#include <conio.h>
using namespace std;
void fillVector(vector<int> &aVector);
// PRECONDITION: number declared size of array a.
// POSTCONDITION: number_used is the number of values stored in a
//a[0] through a[number_used-1] have been filled with nonnegative int.

[code].....

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved