passing an iterator to a function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: passing an iterator to a function

  1. #1
    Join Date
    Nov 2010
    Posts
    146

    passing an iterator to a function

    Hello all

    Is there anyway I can pass an iterator to a function?

    Thanks

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,112

    Re: passing an iterator to a function

    What's your problem? You can surely pass an iterator to a function.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Re: passing an iterator to a function

    Yes,

    What's the problem?
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Nov 2010
    Posts
    146

    Re: passing an iterator to a function

    Actually passing an iterator also wont do. I have vectors of different struct types. A "space separated" string is stored in one of the struct variables (all of type string) in each vector. I have to remove spaces from the "space separated" string and replace it with a comma to make it a csv string but except at one place i-e I want to keep that substring still a "space separated" string and I have also stored that "space separated" string separately in one of the struct elements.

    e-g
    Code:
    element-1"space separated" string part of the string: "abc 123" which is for example the fifth/sixth substring in the example below 
    element-2 of vector-structtype-1: "shnd 23j l94 hsi8 abc 123 md38 fhw0 8fdj"
    csv element-2 of vector-structtype-1: "shnd, 23j, l94, hsi8, abc 123, md38 fhw0 8fdj"
    
    
    element-1 of vector-structtype-2: "space separated" string part of the string: "klm 987" which is for example the fifth/sixth substring in the example below 
    element-2 of vector-structtype-2: "dnhs j32 49l 8ish klm 987 83dm 0wfh jdf8"
    csv element-2 of vector-structtype-2: "dnhs, j32, 49l, 8ish, klm 987, 83dm, 0wfh, jdf8"
    The code below works fine but I dont know how to keep that specific substring still a "space separated" string.

    Code:
    void liffe_breach::CSVline(int Count, int ordIDno, string str) // I want this str to be an elements of a specific vector. Is it possible?
    {									// because vectors are of different (struct) types
    	std::stringstream Brchno;
      	Brchno << Count;
      	
      	std::stringstream IDno;
      	IDno << ordIDno;
      	
      	stringstream ss(str);
      	string word;
      	string cvsLine = "";
      	while ( ss >> word )
            {
      		cvsLine = cvsLine + word + ",";  
          	}
    }

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,359

    Re: passing an iterator to a function

    You can temporarily replace the spaces you don't want to transform into another character (for example "_"), run your program, and then re-insert the space back in:

    Code:
    Step 0: "shnd 23j l94 hsi8 abc 123 md38 fhw0 8fdj"
    Step 1: "shnd 23j l94 hsi8 abc_123 md38 fhw0 8fdj"
    Step 2: "shnd, 23j, l94, hsi8, abc_123, md38, fhw0, 8fdj"
    Step 3: "shnd, 23j, l94, hsi8, abc 123, md38 fhw0 8fdj"
    EDIT: You know, you wouldn't be having all these problems if you were actually modeling your data, instead of always trying to do string operations.
    EDIT2: perl (and regexes in general) is much better suited for this task. This is a 3-line perl script.
    Last edited by monarch_dodra; February 23rd, 2011 at 06:03 AM.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,646

    Re: passing an iterator to a function

    Is it always the same field that you want to keep the space ? If so,
    you can keep a counter to determine whether to keep the space
    or put a comma.

    Code:
        std::stringstream Brchno;
        Brchno << Count;
        
        std::stringstream IDno;
        IDno << ordIDno;
        
        stringstream ss(str);
        string word;
        string cvsLine;
        int field = 0;
        while ( ss >> word )
        {
            cvsLine = cvsLine + word;
            if (field == 4)
                cvsLine += " ";
            else
                cvsLine += ",";
    
            ++field;
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center