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


ADVERTISEMENT

C++ :: Cannot Push Back String At The End Of Vector

May 23, 2013

I have below classes

Code:
class moClassValueContainer {
public:
moClassValueContainer();
moClassValueContainer(string,int);
[Code] ....

In my main.cpp, I have blow loop

Code:
for (xml_node tnode = it->first_child(); tnode ; tnode = tnode.next_sibling()) {
Container tmpContainer(tnode);
if (tmpContainer.getType() == SINGLE) {
string t = tmpContainer.getName();

[Code] ....

I cannot push_back(t). I examined the code with debugger, t has correct string value assigned, but even after 20-30 iterations, there is no element for headerFields vector.

View 1 Replies View Related

C++ :: Vector Of Pointers - Push Back Objects / Constant Parameters

Apr 11, 2014

I have a class called Question:

#include <string>
#include <vector>
using namespace std;
class Question {
string title;
vector<Thing*> posAns;
vector<Thing*> negAns;

[Code] ....

error: no instance of overloaded function 'std::vector::push_back()' matches the arguments list
argument types are (const Thing *)
object type is: std:: vector<Thing *, std::allocator<Thing *>>

So it cannot be constant, what if I just leave it non-constant? Will it be safe?

View 2 Replies View Related

C/C++ :: Vector Contents Unexpectedly Different Within The Same Function

Jun 14, 2014

I am trying to create a tennis tournament simulator. There are 128 players in the tournament, and 32 seeds. I have a vector filled with 'Player' objects named 'Players'. Each 'Player' object has three member variables, Strength, Ranking, and ATPPoints. When the vector 'Players' is first declared, the default constructor for each 'Player' object is used, which initialises each of the 'Player' objects to zero. I then generate a Strength value in the range of 0-10 for each of these 'Player' objects, and sort them descendingly according to their strength.

I now want to create the draw for a tournament. I declare a vector 'Draw' with 128 'slots'. Each slot initially contains a blank 'Player' object, with its member variables set to 0. I first place the top 32 players throughout the draw such that they do not meet until later rounds as they would be in real life. For the remaining players, I position them in the draw by generating a random number between 0 and 127. Some of the slots 0-127 are occupied by seeded Players. These seeded Players have non zero Strength and Rank values, so to avoid overwriting these slots, I check the Strength value of the 'Player' object in the slot in question. A return value of 0 would indicate an empty slot.

At the beginning of the function, I output the Strength and Rank values of the 'Player' objects already in the draw. This outputs correctly.

for (Player& p : Draw)
{// outputs the draw in it's current state, with the already placed seeds as expected.
cout << p.GetRanking() << " " << p.GetStrength() << endl;
}

[Code]....

View 7 Replies View Related

C++ :: How To Push Data Into The Vector

Apr 20, 2013

How can push data into the vector <list<Edge>> adjList? adjList[n].push_back (e);//gives error.

struct Edge {
int from_node_number;
int to_node_number;
int weight;
};
vector <list<Edge>> adjList;
Edge e;
for (int n=0; n< graph.size(); n++)
adjList[n].push_back (e);

View 6 Replies View Related

C++ :: Converting Integer To Vector And Back

Feb 15, 2012

I'm able to convert an integer to a vector<unsigned char> and back. However, when I try to use a nearly identical function designed for the long long data type, the last byte or two is broken.

Program code:

long long num = 9223372036854775551LL;
cout << "Before: " << num << endl;
vector<unsigned char> data = getBytes(num);
num = getLongLong(data);
cout << "After: " << num << endl;

Code for converting between vector<unsigned char> and long long:

Code:
vector<unsigned char> getBytes(long long value) {
int bytes = sizeof(value);
vector<unsigned char> data(bytes);
for (int i = 0; i < bytes; i++)
data.at(i) = (unsigned char)( value >> ((bytes-i)*8) );

[Code] ....

Output:

Code:
Before: 9223372036854775807
After: 9223372036854775552

Is there something special about long long that would prevent this from working? This problem doesn't happen with int.

View 3 Replies View Related

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 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++ :: Printing Contents Of A String That Also Exist In A Vector

Jan 27, 2014

For the last part of this problem, if player 2 loses the game of hangman, I need to display the letters they did get right. In order to do that, I believe that I need to traverse vector v for the elements that exist in the string hiddenword, and then print those characters that match.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;
int letterFill (char, string, string&);

[Code] ....

View 1 Replies View Related

C++ ::  how To Use Push Back In 2D Vectors

Jul 31, 2013

I am trying to use push back in a 2D vector but I don't know how to. This is what I have:

vector <vector <BigInt> > matr;

for (BigInt i=0;i<rij;i++) {
for (BigInt j=0;j<kolom-1;j++) {
matr.push_back().push_back((i+1)^(pow-j));
}
}

I quickly invented something but that doesn't work obviously. it should be equivalent to this: (the only problem in the code below is that those indexes don't exist yet that's why I need push_back())

for (BigInt i=0;i<rij;i++) {
for (BigInt j=0;j<kolom-1;j++) {
matr[int(i)][int(j)]=(i+1)^(pow-j);
}
}

View 2 Replies View Related

C++ :: How To Randomize Answer Order With Push Back

May 8, 2014

How to now randomize the answer order within this code ? I only included the relevant piece of code of the task in question.

questions.push_back(
"Question [1]"
" "
"What port does HTTPS use ? "
"1. 25 "
"2. 443 "
"3. 23 ");

answers.push_back("2");

View 3 Replies View Related

C++ :: How To Avoid Using Backspace Character With Push Back

Mar 28, 2014

How to avoid using Backspace character with push_back? I'm making a software as an ATM, so when the user try to enter the password the user only sees *******, but when trying to delete it doesn't delete a character. It just adds a new one. Here's my code:

string password2 = "";
cout << "PASSWORD: ";
ch = _getch();
while(ch != 13) //character 13 is enter {
password2.push_back(ch);
cout << '*';
ch = _getch();
}

And also, I try to use pop_back(); but it doesn't work either.

View 6 Replies View Related

C++ :: How To Pass Array To Function And Return It As Sorted Vector

Sep 17, 2014

How to pass my array to the function and return it as a sorted vector. I'm kind of lost with the functions part.

Problem: Write a program that performs a sorting routine as discussed in class. Create an array of 10 integers (e.g., 63, 42, 27, 111, 55, 14, 66, 9, 75, 87). Pass this array to a separate function that sorts the integers and returns a vector containing the sorted integers. Use either the SELECTIONSORT algorithm or the INSERTIONSORT.

Code so far:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>

[Code]...

View 2 Replies View Related

C++ :: Incorrect Vector Output Loop Error

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

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++ :: 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/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++ :: Game Of Pick 4 - How To Find Similar 4 Digit Numbers In Array Of 120

Jun 6, 2014

I have a question regarding finding similar 4 digit numbers in a pool of 120 numbers

Below is an linear single dimension array of 4 digits

There are 120, 4 digit numbers in total

My question is this - How can I code in C - a function that looks through all the 120, 4 digits to find similar numbers

Example - 2095 is similar or matches to - 0950, 5095, 5250, 5269 - i.e having 3 of the digits that are the same in the 4

The code must print out 2095 + all the matched numbers

If the Matched or Similar numbers are less then a certain number n - that number is discarded and the code should go onto the next number

2095095053741884864037233464846523768035
4340520865405306753553226100610753235081
1160346508409698809176715645765520676974
2398509523307591808464215318649140982136
2388015030217584311064844010520796345135
5376565155806436092330366745536969232311
4351519149310340620918615194324744655250
5330634052450976531053882380318069765623
2186677440212394367468519636617536556706
5274239549814534091052060118499521655275
6351091153944834003545212360098053955218
4835406061305276769161885611376776845269

I have written some code below but it is not working ...

Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <memory.h>
#include <dos.h>
FILE *fileptr;
FILE *fileptr1;

[Code] ....

View 10 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 :: Transferring Contents Of One Character Array To Another Function

Feb 1, 2014

In the function *expandArrayList and *trimArrayList I need to allocate either more memory or less memory to the given array. Right now I am using malloc on a new array and then transferring the elements of the original array to the new array, freeing the original array, and then reassigning it to the new array so it's size and capacity are either bigger or smaller than the original. I feel like there has to be a simpler way to do this with either realloc or calloc, but I cant find sufficient information on either online. Would I be able to use either to make this easier and the code cleaner?

Also, these are character arrays that contain several character arrays inside them, so I was wondering what the best way to transfer the contents of one array to the other would be. I have gone between

Code:

//example
for (i=0; i<length; i++){
newarray[i] = oldarray[i];
}
and
Code: for (i=0; i<length; i++){
strcpy(newarray[i], oldarray[i]);
}

but I'm not sure which one (if either) should work.

The 'ArrayList.h' file that is included contains the structure ArrayList, as well as the function prototypes for the functions listed below.

Here is the structure in ArrayList.h:

Code:

#define DEFAULT_INIT_LEN 10
typedef struct ArrayList {
// We will store an array of strings (i.e., an array of char arrays)
char **array;

[Code] ....

Here is my code:

Code:

//included libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ArrayList.h"
//Create arraylist

[Code]....

View 3 Replies View Related







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