Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. reading values within std::list types

reading values within std::list types

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jon 80
    wrote on last edited by
    #1

    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

    W M 2 Replies Last reply
    0
    • J jon 80

      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

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      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 call Sentences[10].

      J 1 Reply Last reply
      0
      • W Waldermort

        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 call Sentences[10].

        J Offline
        J Offline
        jon 80
        wrote on last edited by
        #3

        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

        W 1 Reply Last reply
        0
        • J jon 80

          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

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • J jon 80

            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

            M Offline
            M Offline
            Mike Danberg
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups