I am storing info in a vector but I want to store certain info at a particular index. I am new to using vectors and am unsure about have to do this. I am aware of the insert method but am confused on how to use it to store at a particular index.
Consider I have a vector of strings and then I use an istringstream to read each word of each element in the vector, why do I nescessarily use an istringstream?
This is the code that does what I just described (I think)..
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.
I'm having a problem filling a vector from a file. Basically, it is adding an empty element at the end. I'm new to Qt and haven't worked with file streams much so how to stop the stream before it adds the extra element.
But, if I add another element to the vector and write that the file look like this.
//file after adding element 132654 0 02132014 132654 0 02132014 0 0 132998 22 02202014
I have it set to append at the moment so that is why the first line is repeated. I figure the problem is with if(in.atEnd()). I could fix it by deleting the last element right after adding it, but that seems like more of a hack than anything else.
I want to sequentially remove one element at a time starting with the first. When the second element is removed, the first element needs to go back in. The sequence would look like
Code: // original vector, row_numbers.size()=9 row_numbers{1,2,3,4,5,6,7,8,9}; // trimmed vector, row_numbers_trim.size()=8
[Code] .......
I have been working under the assumption that the best method would be to have row_numbers remain untouched and work on a copy. For each step in the sequence, you would create row_numbers_trim as a copy of row_numbers, and then remove an element from row_numbers_trim.
Code: // position being removed int counter = 0; // copy original vector row_numbers_trim = row_numbers; // remove the first element from the copy row_numbers_trim(row_numbers_trim.begin()+counter);
All you would have to do here is to increment counter in a loop. is there a better way?
I want to select 1 element from each vector without duplication of any combinations. Essentially only when all combinations are done with 1st element in first vector ,only then it should move to next element in first vector.
Say i have elements :[123] [456] [789]
my combinations should be like 147 148 149 157 158 159 167 168 169 247....
Also, I cant have any repetitions and only after all combinations of 1 are done only then the loop has to move to next combination ie 247 combination and so on.
I tried NCK (n choose k) command but it gave me random combinations. How should i go about it with using minimal for loops?
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?
i have been fiddling with pointers but I don't understand how the proper syntax is written when I want to acces an element of an array through a pointer to a pointer...The code is all mostly just random bs for learning purposes. I marked the problem "// THIS LINE"
I need to access the functions of the derived classes from a vector of objects of base classes (can't believe I wrote it). Here a Diagram for you to understand:
So as you see, I need the function Use() from the Usable class, to be able to be called from the vector like:
So I have linked list and function which deletes element if next element is bigger, so my code is working but its not working with first element, in the comment I have wrote code which I would code for checking that first element, but when ever I check it is blowing up all program.
#include <iostream> using namespace std; struct llist { int x; llist *next;
I have a global list that contains smaller lists of char arrays. I have an issue where when I'm reading back the inner lists the last element of one list seems to point to first element of the next.
So my data looks like the below (values separated by commas with the pairs separated by tabs. The last pair in a line is the same as the first). When I read the first list back instead of seeing "456.678,678.98" as the last element in the list. I see "435.67,234.98" twice: at the end of the first list and start of the other. I have debugged when the list is populated and can see the correct values going in so I can't figure what's happening.
obstacle_list = op_prg_list_create(); while (fgets(line, sizeof(line), obstaclePositions_traj_file) ) { token = strtok(line, " "); //Pull the string apart into tokens using the
I have an std list of type double.. and the list is always guaranteed to have just 2 elements. I need to get the value of element 2 minus element 1. What is the least amount of code to accomplish that?
I tried this:
Code: list<double> dList; dList.push_back(1.0); dList.push_back(2.0); list<double>::iterator iter = dList.begin(); list<double>::iterator iter2 = dList.end(); double result = *iter2 - *iter;
if we don't provide the acces modifiers for base class and we need to manipulate the private data of base class in derived class. Is there anyway to acces the private data members? Here's a coding example
class A { private : int a; }; class B : public class A { public : void displayA() { cout<<a<<endl; } };
how i can acces the a of base class A in derived class B without acces modifiers.
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)
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.
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:
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.
I'm making my first steps in STL, and I have a few question:
Is there a way to get an iterator to the i'th element in the collection (set or list), instead of just to the end or the begin?
And another question: Let's say I have an iterator, pointing to some element in my collection, and I use erase() (which takes as parameter an iterator that points to the soon-to-be erased element), what happens to that iterator? will it now point to NULL?