reading values within std::list types
-
I am trying to retrieve values from within a list of strings SentenceList.h ---------------------------------- #pragma once .. #include ... using std::list; ... class CSentenceList { .... string display(unsigned int iLineNumber = 1); list Sentences; //list of sentences // Note: MOVE TO PRIVATE AFTER IMPLEMENTING display .... }; The list is being created and I would like to create a method 'display' that returns a string according to the line number. The method should loop through the list and return a string according to the value of iLineNumber. I have tried using Sentences.front, but I could not picture the correct return value to be read to return the string value. How do I read values from Sentences? Jon
-
I am trying to retrieve values from within a list of strings SentenceList.h ---------------------------------- #pragma once .. #include ... using std::list; ... class CSentenceList { .... string display(unsigned int iLineNumber = 1); list Sentences; //list of sentences // Note: MOVE TO PRIVATE AFTER IMPLEMENTING display .... }; The list is being created and I would like to create a method 'display' that returns a string according to the line number. The method should loop through the list and return a string according to the value of iLineNumber. I have tried using Sentences.front, but I could not picture the correct return value to be read to return the string value. How do I read values from Sentences? Jon
there you have create an instance of string with size 1. That is the equivlent of
char str[1];
So where are the strings that you want to return? For the list object, just treat it like an array. If you want the 10th string just callSentences[10]
. -
there you have create an instance of string with size 1. That is the equivlent of
char str[1];
So where are the strings that you want to return? For the list object, just treat it like an array. If you want the 10th string just callSentences[10]
.When I tried the following, the compiler didn't like it... string CSentenceList::display(unsigned int iLineNumber) { return Sentences[iLineNumber]; } Error: (51): error C2676: binary '[' : 'std::list<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator with [ _Ty=std::string ] Jon
-
When I tried the following, the compiler didn't like it... string CSentenceList::display(unsigned int iLineNumber) { return Sentences[iLineNumber]; } Error: (51): error C2676: binary '[' : 'std::list<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator with [ _Ty=std::string ] Jon
Oh, sorry my fault, I was confusing list with vector. You are using list, but want to provide a line number. You do realise a list is sorted. Everything you add to it will be rearranged. If you want to add sentences one ata time and keep them in order you should really use the vector class. If you really want to use the list then you will have to go through a loop testing the value of each sentence, but there is no way for you to know which line goes with which sentence.
-
I am trying to retrieve values from within a list of strings SentenceList.h ---------------------------------- #pragma once .. #include ... using std::list; ... class CSentenceList { .... string display(unsigned int iLineNumber = 1); list Sentences; //list of sentences // Note: MOVE TO PRIVATE AFTER IMPLEMENTING display .... }; The list is being created and I would like to create a method 'display' that returns a string according to the line number. The method should loop through the list and return a string according to the value of iLineNumber. I have tried using Sentences.front, but I could not picture the correct return value to be read to return the string value. How do I read values from Sentences? Jon
You need to use an iterator to traverse the elements in the list. Try something like: list::iterator start = Sentences.begin() while ( start != Sentences.end() ) { if(sentence matches) // probably want to try something like string == (*start) { //do something break; // if you hit a match break } start++; } Hope that helps. There are plenty of articles on using iterators as well. -- modified at 23:54 Sunday 27th August, 2006