Assuming I have a list of pointers to a generic type T:
#include <vector> //... list<T*> myList;
Now assuming I want to go on the list, and if T's type is matched to the type I'm looking for, then cast it to this type and do something. List shown here:
When using an iterator with a std container (list / vector etc) sometimes it's possible to modify the container (e.g. delete an item) yet still carry on using the iterator - whereas in other cases, modifying the container immediately invalidates any open iterators on it. Is there an easy way to know which containers fall into which category? (or does it vary from one compiler to another?)
I am working on very large code. I got a segmentation fault when trying to use one cpp file and tried to locate the error using Valgrind
Since the code is very large, I will only post a short portion of it below. I think the problem may come because triann is a vector defined in the header class, so triann[tri] is causing problems?
void ADe::aNe(int v, set<int> &nei) { for(set<int>::iterator iter = vert2tri[v].begin(); iter != vert2tri[v].end(); iter++) {
I'm working on writing some classes around a ROM hardware addon card. The classes expose the data on the ROM as a container with iterators, much like a vector or a list.
The classes don't have any data themselves, since all the data is on the ROM.
I'm having some dillemma's as to how to approach/implement the classes. If you were to write somethign like this... Or were using something like this written by someone else.... How would you expect this to be done ?
1) Make all the member functions static, make a private constructor to prevent making instances. This works, but may look a bit weird...
Code: for (auto it = RomTable::begin(); it != RomTable::end(); ++it)
2) expect users to make a (dummy) instance, then use it as a regular container. this might be a bit counter intuitive since the class has no datamembers.
3) create a single instance, expect users to use that everywhere. make the constructor inaccessible. Some C++ 'purists' might perceive this as global data and thus not a good solution ?
Additionally. Do I need to provide both a const_iterator and an iterator ? There's nothing to be modified, so I'm guessing an iterator isn't needed (?) Or will some STL stuff not work without an iterator ? I'm obviously not fussed about the STL functions that make changes to the container to not work (like sort, fill, swap...)
So i made an STL compatible container.And to make this work I had to make my own iterator (derived from std::iterator).
What is the portable (if any) and "well behaved" thing to do in case of usage anomalies.such as iterating an iterator too far, or passing an invalid index to a operator[]
Looking at how VC++ does things in something like std::array or std::vector.
Code:
iterator_type& operator+=(difference_type offset) {// increment by integer #if _ITERATOR_DEBUG_LEVEL == 2 if (size < index + offset) {// report error
[Code] .....
lots of names starting with underscores, so it's implementation specific. Is there even a "well behaved" thing to do ? Or is any such work always going to be compiler specific?
I have a 'Graph' class, which has derived classes for Adjacency Matrix and Adjacency List representations.
How do I provide iterators for traversing vertices and edges, when the iterator classes would have different implementations for the different derived classes ?
The following way is the only one I can think of, but seems quite cumbersome.
Or is there a pattern for doing this that I'm not aware of ? Would composition be a better idea here compared to polymorphism ? I mean, I can think like..a Graph can 'have' several representation 'objects' within it.
All the involved classes are templates,not sure if that makes the situation different.
I get this error when i try to run this code for an inventory in debug mode in VS. But for some reason it works just fine in release mode.
void Push_Back_Item(Item *item){ for(int y = 0; y < InvSizeY; y ++) for(int x = 0; x < InvSizeX; x ++){ auto Iter = ItemList.find(std::make_pair(x,y)); if(Iter != ItemList.end()){ item->SetDead(); // ERROR } } }
This isnt the full code though but it still gives me the same error.
The only thing "item->SetDead()" does is to set a bool to true.
This is the map i get the iterator from std::map<std::pair<int,int>,Item*> ItemList;
This have been bugging me for quite some time now.
write a c++ program that reads an unknown number of integer values and then print count, sum and average of odd values, even values, positive values, negative values!!
I've been working on a homework assignment that randomly generates integers and populates them into an array (array1). The program is then supposed to:
1.) copy those values to a second empty array (array2)
2.) sort the values already in array1 (using an inline function)
3.) enqueue the unsorted integers from array2 into a heap vector
4.) a third empty array (array3) is supposed to be populated with those unsorted integers (by dequeuing them from the heap), sorted in reverse order.
But no matter what I do, I always get garbage values like these:
I've tried using both a standard random number generator:
array1[i] = rand()%100+1;
And the d_random.h file my instructor gave us, but nothing works.
Here's the code from all 3 files:
HeapTester.cpp
Code: #include <iostream> // Provides cin, cout #include <cstdlib> // Provides EXIT_SUCCESS, rand, srand #include "d_random.h"//Provides random number generator #include "Heap.h" using namespace std; // Use C++ Standard namespace //Elements in each array. const int arrayLength = 15;//100;
I was wondering if this was even possible and if so, how do I do it.
else if (speech.ToLower().Contains("truck") && speech.EndsWith(number)) { Here I would like to see if my speech had ended with any of the values i would have stored in the string "numbers". If it did, I would like to just take the value and add it to a new string called whatever }
I have tried this a million different ways and I cant get it to work. I'm not even sure how I would go about storing tons of different numbers in one string, or if that's even possible.
Goal: Write a program that compares the values stored in the first array to the user inputted values in the second array.
In order to fix this error: [URL]...
I had to change my array initialization to one with a star in front of it:
char a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"}; to: char *a1[]={"a","d","b","b","c","b","a","b","c","d","a","c","d","b","d","c","c","a","d","b"};
I also changed my 2nd array to one with a star in front of it: char *a2[20];
What does this mean exactly? Putting a star in front of an array?
Also, I am now getting an "unhandled exception" when I try to get input for my 2nd array:
How can I display my inputted values here without overwriting the other earlier inputted values. It should be displayed like this [URL].... but mine shows this [URL].... I've been stucked here for hours in thinking for an algorithm. Everything is working fine except the PDISPLAY part
I'm a programmer on my high schools FIRST Robotics team. to give a bit of backstory if you don't know, we use C++ to program a 120 lb robot to compete in a competition released every year.
NOW - why I want to mess with an xml file. When we're messing with variable values to make sure everything moves at the right speed, stops at the right place, etc., we have to:
1. Change the file 2. Build code 3. Deploy code to the robot 4. Reboot the computer on the robot (FRC uses a 4 port cRIO by NI, takes ~ 50s to reboot) 5. Reconnect.
All in all, its about a 3-3.5 minute process to change values. Because of this, I would love to be able to store all my motor speeds, stopping positions, etc., in an xml file then simply ftp to the robot, mess with the xml, save it, and re-run the program.
SO! How would I even begin this process? I just want learning how to use one of those parsers like xerces to end up realizing that I can't use it in an application such as this.
The problem is with that a. What is it, a pointer? What's the difference between the a in the main function and the a in the function?
#include <iostream> #include "Header.h" using namespace std; short int capacity(int* a) { int capacity;
[Code] ....
The function it returns i think the size of the pointer instead of returning the size of my array. I don't think i fully understood pointer arithmetic.
I am having problems writing this program for class the program must read a DAT file and print the Array and show the sums of A and B and the smallest and largest of the numbers in the DAT file
this is what it is suppose to look like after compiled
Array A[]: 60 40 80 100 30 20 90 50 10 70 Array B[]: 15 95 35 -10 45 -5 85 75 65 55 -20 25 5 25 -15 Sum of the elements of A[] = 550 Sum of the elements of B[] = 475 The smallest element of A[] = 10 The smallest element of B[] = -20 The largest element of A[] = 100 The largest element of B[] = 95
but this is what I get..
Array A[]: 60 40 80 100 301845634224184564560018456342248463648 1 Array B[]:3994112184685425110290441628889523994216 0 03994108399426418511697 04 -1184564557518455763132047600601153318 Sum of the elements A[] =1250410711 Sum of the elements B[] =-475173273 Smallest element of A[] =1845634224 at index 5 Smallest element of B[] = 0at index 5 Largest element of A[] = 1 at index 9 Largest element of B[] =1851169704at index 9