- "stl - C++: How can I stop map's operator[] from inserting bogus values? - Stack Overflow" - 5 Updates
- Swearing in Usenet posts - 1 Update
- Parking Lot with Alley Classes Using Eclipse - 3 Updates
- std::string Question - 2 Updates
Lynn McGuire <lmc@winsim.com>: Jul 14 07:51PM -0500 "stl - C++: How can I stop map's operator[] from inserting bogus values? - Stack Overflow" http://stackoverflow.com/questions/10684888/c-how-can-i-stop-maps-operator-from-inserting-bogus-values Looks like the fix is to use find () instead of []. Am I missing anything here ? Thanks, Lynn |
Lynn McGuire <lmc@winsim.com>: Jul 14 09:04PM -0500 On 7/14/2016 7:51 PM, Lynn McGuire wrote: > Am I missing anything here ? > Thanks, > Lynn And I found my bug ! And lo, the bug was causing new members of the std::map to be created, totally confusing the issue. for (std::map <int, int>::const_iterator i = itemIndexes.begin (); i != itemIndexes.end (); i++) { // the index is the key of the pair int itemIndex = i -> second; DataItem * item = ownerGroup -> getItem (itemIndexes [itemIndex], itemRelationships [itemIndex]); if (item) item = item -> inputOff (ownerGroup); } The bug was: int itemIndex = i -> second; needs to be: int itemIndex = i -> first; Thanks, Lynn |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jul 15 04:37AM +0200 On 15.07.2016 04:04, Lynn McGuire wrote: >> http://stackoverflow.com/questions/10684888/c-how-can-i-stop-maps-operator-from-inserting-bogus-values >> Looks like the fix is to use find () instead of []. >> Am I missing anything here ? Dunno, but `at` is a nice function, and `const` prevents modification. You can't use `[]` with a `const` map, but you can use `at`. > if (item) > item = item -> inputOff (ownerGroup); > } Note, one `index` but two `indices`. I believe. ;-) Also note that there's no point in updating `item`, except to support debugging, since its scope ends just after. I think I'd express that loop like this: for( auto const& pair : item_indices ) { int const i = pair.first; if( Data_item* const p_item = owner_group->item( pair.second, item_relationships.at( i ) ) ) { item->input_off( owner_group ); } > int itemIndex = i -> second; > needs to be: > int itemIndex = i -> first; Cheers!, - Alf |
Lynn McGuire <lmc@winsim.com>: Jul 15 01:06AM -0500 On 7/14/2016 9:37 PM, Alf P. Steinbach wrote: >> int itemIndex = i -> first; > Cheers!, > - Alf The variable item can have multiple owners. If so, it will get copy on written and get a new address. I set item to that new address just in case I want to debug it. Lynn |
"Öö Tiib" <ootiib@hot.ee>: Jul 15 03:40PM -0700 On Friday, 15 July 2016 03:51:54 UTC+3, Lynn McGuire wrote: > http://stackoverflow.com/questions/10684888/c-how-can-i-stop-maps-operator-from-inserting-bogus-values > Looks like the fix is to use find () instead of []. > Am I missing anything here ? The 'std::map' template is IMHO actually *overburdened* with element-searching operations. There are 'std::map::operator[]', 'std::map::find', 'std::map::lower_bound' and 'std::map::at'. So real question is perhaps when to use one or other from these 4. My impression is that best is to use 'at' when lack of searched element is exceptional (or "impossible"). If lack of searched element is normal then we should choose one of other three depending on what we do when element wasn't found. When we never insert the missing element then best is 'find'. When we always insert the missing element then best is 'operator[]'. When something about other 3 operations did not suit us (for example that we only sometimes insert that missing element) then 'lower_bound' (and 'emplace_hint') is likely the tool. |
Real Troll <real.troll@trolls.com>: Jul 15 07:15PM -0400 On 14/07/2016 21:49, Gareth Owen wrote: >>> But I'm also with Nusrat Fateh Ali Khan. >> Do you also support Sharia Law? > Jude's sister, and Denis's daughter, I believe. These members of the peace loving religion have killed nearly 84 in France and they will kill 1000s in Turkey now they are in charge. What will happen to NATO who have HQ in Turkey? Will Allah protect it? |
Denise James <denisetoo@gmail.com>: Jul 15 08:51AM -0700 Hello, I am getting an error in Eclipse, error: expected initializer before 'CAlley', along with warnings such as "will be initialized after [-Wreorder]" Below is the code: (I get an errror on " CAlley AlleyA, AlleyB; // Create two classes of alley" line) #include <iostream> #include <iomanip> // to use setw object#include <iostream> #include <cctype> #define MAXSIZE 5 using namespace std; // to use cout and cin objects class CarNode { public: CarNode() : m_pNext(0), m_ticketNum(0) { }; ~CarNode(); CarNode(CarNode &):m_pNext(0), m_ticketNum(0) { }; // assign next pointer void SetNext(CarNode* p){m_pNext=p;} // assign ticket number void SetTicketNum(int tN){m_ticketNum=tN;} // get the next pointer CarNode *GetNext(void){return(m_pNext);} // get the ticket number int GetTicketNum(void){return(m_ticketNum);} private: CarNode *m_pNext; // pointer to next node in stack int m_ticketNum; // ticket number of car }; class CAlley { public: CAlley () : m_pTop(0), mSize(0), mMaxSize(MAXSIZE) { } ~CAlley () {} CAlley (CAlley &):m_pTop(0), mSize(0), mMaxSize(MAXSIZE) { } int Park(int); // park a car void Retrieve(int userTicketNum, CAlley *pB); // retrieve a car // void Terminate(); // quit the program void Display(char *); // display contents af alley void SetTop(CarNode *p){m_pTop=p;} // assign top pointer // check if stack is empty bool Empty(){return ((mSize==0) ? true : false);} // check if stack is full bool Full() {return ((mSize==MAXSIZE) ? true : false);} int Push(CarNode *); // push one node onto top of stack CarNode * Pop(); // pop one node from the top of stack CarNode *m_pTop; // pointer to top of alley (stack) private: int mSize; // number of nodes in alley (stack) int mMaxSize; //max number of nodes in alley (stack) }; //////////////////////////////////////////////////////////////// // Function: CAlley::Push // Purpose: Add a new node to top of stack // Parameters: // CarNode * pNewNode- the node to be added to top of stack // Local Variables: // status - return 1 if pushed sucessfully // - return 0 if stack was full //////////////////////////////////////////////////////////////// int CAlley::Push(CarNode* pNewNode)// type integer function { if(Full()){ cout << "PARKING LOT IS FULL"; return 0; } else{ m_pTop = pNewNode; // make the top node equal to passed (*pNewNode).SetNext(m_pTop); // Create the next node m_pTop = pNewNode; // Assign the next node to the top return 1; } } ///////////////////////////////////////////////////////////////// // Function: CAlley::Pop // Purpose: Remove a node to top of alley (stack). // Parameters: // CarNode * pNewNode- returns the node removed from top of alley // is zero if stack is empty // Local Variables: // status - return 1 if pushed successfully // - return 0 if stack was full ///////////////////////////////////////////////////////////////// CarNode *CAlley::Pop() // type CarNode function { if(Empty()){ cout<<"PARKING LOT IS EMPTY"; } else{ CarNode *pNewNode; pNewNode = m_pTop; m_pTop = (*pNewNode).GetNext(); delete m_pTop; } } /////////////////////////////////////////////////////////////// // Function: CAlley::Park ( ) // Purpose: Park a car, if lot is not full. First allocate a // node, then add it to the top of the stack // Parameters: // userTicketNum - the ticket number for the node to be added // Local Variables: // CarNode *pNewNode - local pointer to newly allocated node // int status - 1 if parked sucessfully (lot not full) // 0 if not parked (lot was full) /////////////////////////////////////////////////////////////// int CAlley::Park(int userTicketNum) { if (Full()) cout << "PARKING LOT FULL" << endl; else{ userTicketNum++; cout << "Here is your Ticket no. " << userTicketNum << endl; CarNode NewNode; // create a node to hold car NewNode.m_ticketNum = userTicketNum; NewNode.m_pNext = nullptr; // the next node is empty Push(&NewNode); } } /////////////////////////////////////////////////////////////// // Function: CAlley:: Retrieve ( int userTicketNum, CAlley *pB) // Purpose: Retrieve a car from alley A. Search for the car/node // based on ticket num. by driving a car (popping off top) out of // A and driving (pushing onto top) into B. // If the car is found, it is not pushed onto B, but rather, // it is deleted. Then the cars in B are moved back into A. // // Parameters: // userTicketNum - the ticket number for the node to be added // pB - pointer to CAlley B // // Local Variables: // CarNode *pCurr - local pointer used as index // int found - 1 if car is found, 0 if not found /////////////////////////////////////////////////////////////// void CAlley::Retrieve(int userTicketNum, CAlley * pB) { // This makes function to be called at Alley A location int topTicket; bool found = false; CarNode* pHoldNode; // create a node to hold the node examined while(!Empty() && !found)//while alley A is not empty // and ticket not found yet { pHoldNode = Pop(); topTicket = (*pHoldNode).GetTicketNum();// access the ticket number if (topTicket == userTicketNum) // if matches exit while loop found = true; else (*pB).Push(pHoldNode); }// end of while if (!found) cout << "CAR NOT PARKED IN MY LOT\n"; while (!(*pB).Empty()) { pHoldNode = (*pB).Pop(); Push(pHoldNode); }//end of while } void CAlley::Display(char *) { cout << "Alley A: "; CarNode * pCurr = m_pTop; while (pCurr != nullptr) { cout << pCurr->GetTicketNum(); if(pCurr->GetNext()!=nullptr) cout << " "; pCurr=pCurr->GetNext(); } cout << endl; } void Terminate() { } int main() { int ticketStub = 123; //ticket stub is the pointer char input ; //User inputs whether to Park, Display or Retrieve Car(s) //Determines row of matrix CAlley AlleyA, AlleyB; // Create two classes of alley cout << "Enter: " << endl; cout << "D to Display the location of the cars" << endl; cout << "P to Park a car " << endl; cout << "R to Retreive a car" << endl; cout << "Q to Quit" << endl; // create five node linked list or stack while (cin >> input) { // If input = Q or q, then exit program. if ((input == 'q') || (input == 'Q')) break; // Validity check for input: If not equal either D,P,R,Q,d,p,r,q // then wait for the next input if ((input != 'D') || (input != 'P') || (input != 'R') || (input != 'd') || (input != 'p') || (input != 'r')) continue; else { switch(input) { case 'P': AlleyA.Park(ticketStub); break; case 'R': int inputTicketStub; cout << "Type in the ticket number: "; cin >> inputTicketStub; AlleyA.Retrieve(inputTicketStub, &AlleyB); break; case 'D': AlleyA.Display(); break; default: ; }//end of switch }// end of else }//end of while input cout << endl << "Adios amigo!" << endl; }// end of main |
scott@slp53.sl.home (Scott Lurndal): Jul 15 06:20PM >Hello, I am getting an error in Eclipse, error: expected initializer before 'CAlley', along with warnings such as "will be initialized after [-Wreorder]" >Below is the code: (I get an errror on " CAlley AlleyA, AlleyB; // Create two classes of alley" line) You should probably take this to your Instructor or their T.A. In the future, post the exact output from the compiler. |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jul 15 11:48PM +0200 On 15.07.2016 17:51, Denise James wrote: > Hello, I am getting an error in Eclipse, error: expected initializer > before 'CAlley', along with warnings such as "will be initialized > after [-Wreorder]" Here are the errors reported by Visual C++ 2015: > original.cpp(23): note: see declaration of 'CarNode::m_pNext' > original.cpp(8): note: see declaration of 'CarNode' > original.cpp(216): error C2660: 'CAlley::Display': function does not take 0 arguments I used the following default options, in environment variable CL: > CL=/nologo /EHsc /GR /W4 /FI "iso646.h" Here are the errors reported by MinGW g++ 5.1.0: > ^ > original.cpp:162:6: note: candidate: void CAlley::Display(char*) > void CAlley::Display(char *) I used the following default options, in command alias g++ (via doskey): > g++=g++ -std=c++14 -pedantic-errors -Wall -Wextra -fexec-charset=cp1252 $* Mostly the options tell each compiler to be a bit more standard-conforming than it is by default, and to produce more warnings, please. It's generally a good idea to use at least two compilers. When one compiler's error messages are incomprehensible, the other compiler might be able to shed some light on the problem. Also, it helps to produce more portable code. Cheers & hth., - Alf |
MikeCopeland <mrc2323@cox.net>: Jul 14 08:43PM -0700 Given: std::string s1 = "Brown, III, Fred B."; I need to (1) determine that there are 2 commas in this string and (2) remove the first comma from the string so that it contains "Brown III, Fred B.". Presently, I parse the CSV elements into a vector, determine that there are 3 elements, and rebuild the string without the first comma. Much work and overhead for what I think this problem needs. 8<{{ I'm seeking some other way(s) to do this, perhaps using string::find or something. However, there's no "find_next_of" in the STL, so this doesn't work. Please advise. TIA --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus |
legalize+jeeves@mail.xmission.com (Richard): Jul 15 03:55AM [Please do not mail me a copy of your followup] MikeCopeland <mrc2323@cox.net> spake the secret code >or something. However, there's no "find_next_of" in the STL, so this >doesn't work. Please advise. TIA find_first_of has an overload that takes the starting position of the search. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com> |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment