C++ :: How To Print Out Elements Of Vector Of Type Pair
Oct 7, 2014
here 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?
View 2 Replies
ADVERTISEMENT
Jan 8, 2013
I 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 Related
Feb 14, 2012
is it possible to alter the Pair function to make it use 3 elements?
View 13 Replies
View Related
Mar 30, 2013
"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 Related
Jan 18, 2015
How can I define type MARKS which will be able to hold 4 elements just like array? I want to access it just like normal array elements using brackets []. In the following struct I want to use such type.
Code:
typedef struct {
MARKS ** pointers;
} ARGUMENTS
void main(){
ARGUMENTS arguments;
[Code] ......
Purpose of the struct ARGUMENTS is to hold 4 pointers to string. I want to mark few positions in string so I can simple access them. E.g. name=John
Code:
arguments[0][0]=0; // begin of the param name
arguments[0][1]=3; // end of the param name
arguments[0][2]=5; // begin of the value
arguments[0][3]=8; // end of the value I mean not to save int but the pointer to the corresponding position.
View 8 Replies
View Related
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
Oct 23, 2013
Use function decomposition.
-To prompt user to enter value and assign it to correct position in array
-calculate the average of an array
-display all element.
-display average.
For some reason, i keep getting error and it doesn't compile. I use Dev-C++
Here is my code:
#include <iostream>
#include <iosmanip>
#include <math.h>
using namespace std;
const int SIZE = 5;
const int LINE = 4;
//function prototypes
void getData(int arr[]);
[Code]...
My error is on line 69. I don't see anything wrong with it.
If i take out setw(3), it doesnt get any error but for some reason it doesnt run. Possible i did something wrong there?
View 2 Replies
View Related
May 2, 2012
I am writing code to access vector elements as, to print first "n" elements of vectors out of its total N elements at a time.
For example, Total vector elements : N= 23
Want to print n elements at time : n =3
How this can be explored using vectors?
View 6 Replies
View Related
Mar 21, 2013
I'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.
View 5 Replies
View Related
Apr 21, 2014
how to swap the first and 'mid' elements of a vector?
View 2 Replies
View Related
Feb 14, 2013
I have an integer vector of size "n".
e.g.
std::vector<int> vec;
vec.pushback(0);
vec.pushback(1);
vec.pushback(2);
vec.pushback(3);
Now I want to generate all possible combinations of size = {0, 1, 2, ... , n}.
{0, 1, 3} is not equal to {3, 1, 0} or {1, 0, 3} or {3, 0, 1}
View 7 Replies
View Related
Apr 9, 2014
I had a question about memory allocation/how iterators work for a std::vector<foo> of a user defined class 'foo'. Say foo contains variables of variable size, so that each member of the std::vector<foo> does not require the same amount of memory space.
Does c++ allocate the same amount of memory for each element, equal to the amount of memory required for the largest element? Or does it use some sort of array of pointers pointing to the location of each element in the vector to make the iterator work? Or does it use some other method? I am wondering because I wrote a code which reads data from a binary files and stores most of it in std::vectors.
The code seems to be using significantly more memory than the sum of the size of all the binary files, and I am using vectors made up of the datatype within the binary files (float). So I was wondering if internally the code was allocating space for each vector element which is the size of the largest element as a way to handle indexing/iterators. I ran my code through a memory leak checker and it found no errors.
View 16 Replies
View Related
May 5, 2013
I am currently trying to implement a 2d vector to store x and y of type int.
I have successfully passed it to the function, however i am unable to store the values in it. It always returns with a segmentation fault and my program terminates from there. May i know how do i store them properly and call them out?
Below is my code snippet
int reconstructSecret(int x, int y, vector< vector<int> > &inVec ,int constructSecret) {
int getX,getY,formula,accum,count,getSecret,startPosition,nextPosition,numerator,denominator;
getX=x;
getY=y;
int result;
[Code] .....
The main method
vector< vector<int> > inVec;
for(int i=0;i<constructSecret;i++) {
cout<<"please key in the "<<i<< "share of x value:";
cin>>x;
[Code] ...
View 9 Replies
View Related
Mar 23, 2015
I am working on a project for class where I use a parent Shape class with circle, rectangle, ect. Classes inheriting from that. Along side these I use a class called Scene. In main I need to create a scene and add some shapes to a vector in scene.
vector<Shape*> shapes
I need to use functions addShape(Shape* shape) and a delete shape method. I have the addShape finished. The problem I am having is with the delete method. Is there a way that I can use something like deleteShape(Shape* shape)? Is it possible for me to delete a specific shape from the vector by passing in that shape, or can it only be done using index? I have looked at the documentation for std::vector as well as std::vector::erase. I am wondering this because if I use index values and do something like
void Scene::deleteShape(unsigned int x) { shapes.erase(shapes.begin() + x ); }
It will lead to some errors later on due the the changing size and indexes of the vector and elements.
View 4 Replies
View Related
May 1, 2015
so lets assume i have a nested vector in a set or vice versa o in a more general form a container in a container example...
Code:
std::set<vector<int> > my_tuple;
How do i access individual elements of lets say the first vector in the set! Or the last element in the 3rd vector?
View 7 Replies
View Related
Dec 28, 2012
I think std::copy appears to do what I'm looking for.
I'm in need of a vector member function that would allow me to "insert" a number of elements from one vector into another vector without resizing or destroying either.
An example of what I'm wanting to do is assign the contents of two[3-5][50-54] to one[7-9][2-6]. Other than looping through both vectors using .at(), is there a way to copy this?
This would be continuous within a user controlled loop with differing elements being exchanged.
typedef vector<vector<unsigned char> > vec;
vec one(10, vector<unsigned char>(10, '1')),
two(90, vector<unsigned char>(90, '2'));
View 4 Replies
View Related
Jun 29, 2014
Acyclic visitor pattern used here to count the number of elements of a certain type in a container.
struct A {
struct Visitor {
virtual ~Visitor() = default;
};
virtual void accept (A::Visitor&) = 0;
[Code] ....
Give the correct:
count = 3
But it is scrappy. The visitor classes had to be placed outside the classes they belonged to, and CountB lost its template because of that. Also, it is very awkward that I have to construct a "reverse hierarchy" for the Visitor classes (which means that this has to bechanged if I ever change the hierarchy for A, B, C, ...). How to improve my solution (using acyclic visitor pattern)? Forward declaring nested classes is not possible in c++.
View 1 Replies
View Related
Jul 19, 2013
I have read that the Erase-remove idiom is the way to go. I have a rough understanding of how this works but am unsure whether I can implement a match-counter along with it.
For counting alone, I would use something like this:
Code:
std::vector<std::string> v; // contains duplicate strings in different elements
std::string term = "foo"; // search term, changing at runtime as well
unsigned int matches = 0;
for( auto e : v ) {
if( e == term ) {
[Code] .....
I'm not sure how (or if) I can combine the two things. That is, I don't know how to integrate a function comparing two (changing) strings into the remove_if() method. Nor do I know how to increment a counter during iteration.
The vector is extremely large, so speed is paramount. I think there are many other avenues for optimization, but decreasing the vector's size for each consecutive search could deliver a big speed boost for subsequent searches I imagine, as traversing it holds the biggest cost.
View 3 Replies
View Related
Mar 5, 2013
I think I need to use
Code:
while(input[0]!='
'){
scanf("%s", input)
printf("%s", pointerhere);
}
basically we were asked to make a program that will print everything before the user hits the enter button. we cannot use fgets and we have to use pointers here.
not sure what exactly to do. We were told not to use fgets cause it's so easy and we need to incorporate pointers in this project..
View 8 Replies
View Related
Nov 18, 2013
I'm working on a program where I have a vector full of <myClassType> structs.
I'm trying to insert items into a vector, searching first through the vector to make sure the value isn't already in the vector before inserting it. The "find" function isn't working properly.
I keep getting C2678 "binary '==': no operator found which takes a left-hand operand of type "myClassType" or there is no conversion errors in Visual Studio 2010.
I know it's something having to do with the find function and my iterators, but I can't, for the life of me, figure out what it is.
I've tried switching to const_iterators, but I get the same error message.
void foo::insert(const myClassType& en) {
vector<myClassType>::iterator it;
vector<myClassType>::iterator it1;
it = find(Table.begin(), Table.end(), en.memberOne);
[Code] .....
View 3 Replies
View Related
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
Apr 5, 2013
I'm trying to print values from a vector of a struct and I don't really know how to do that. Here is a simple code that I have for now to test how to add data to the vector then attempt to print it out.
#include <iostream>
#include <vector>
#include <string>
#include <deque>
#include <fstream>
using namespace std;
struct Employee//employee data
[Code]...
View 2 Replies
View Related
Sep 2, 2013
I'm having problems fully comprehending how to do this task (I'm only going to include my function code since that's the basis of my problem). How should I go about getting my vector to print off 12 items per line. Here's my current function.
void printVec(const vector<int>& v) {
for(unsigned i=0; i < 12;i++)
cout<<v[i]<<" "<<endl;
}
View 2 Replies
View Related
Feb 1, 2013
I am adding int type into vector called "vi" several times like this.
std::vector<int> vi;
void addFunc(int *a, int cnt) {
vi.erase(vi.begin(), vi.end());
vi.clear();
for(int i=0; i<cnt; i++)
vi.push_back(a[i]);
}
and I call the addfunc N times every sec.
And sometimes it has runtime error on vi.push_back line.
(called stack says "_CrtIsValidHeapPointer")
View 5 Replies
View Related
Jun 26, 2014
How do I compare a string vector against an enumerated type? My code so far is:
void convertToLastFirst(vector<string>&names){
enum NameFormat{FIRST, LAST};
for (size_t i = 0; i < names.size(); i ++){
if (names[i] FIRST){
names[i] = LAST;
}
}
}
View 1 Replies
View Related
Aug 16, 2014
I am programming a 2-D platformer video game. The stages are composed of an array (really a vector) of 16x16 px^2 tiles. I have defined a base class "Tile" and several derived classes, e.g., "Ramp", "Door", etc., which have their own attributes. The idea is that upon entering a room, the program will load all of the necessary tile data for that room into a vector. So, I have a vector that looks like: vector <Tile*> room_tiles, and resize it based on the total number of tiles in the room: room_tiles.resize(Tile_Count). I then want to read in certain info from the data file containing all of the tile information for that room. For example, if the data file says Tile 5 should be a ramp, I want to change the 5th element of the room_tiles vector to the derived ramp class. This is really where I'm having trouble. I've worked with vectors of base and derived classes before, but those were always of indeterminate size and I always used something like: (Vector).push_back(new DerivedClass()) to specify the derived class of that element. The problem is that that method only seems to work if you are appending elements to the end of a vector.
how I can do this?
View 7 Replies
View Related