Help searching through a string vector

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shoes2908
    New Member
    • Dec 2007
    • 4

    Help searching through a string vector

    Hi, again I am in a rush and can't seem to remember how to search for a certain string in a string vector... heres my code:

    cin >> worker_num;
    if (cin.fail())
    {
    cin >> name_mod;
    for ( short q = 0; q < list_names.size (); q++)
    {
    if ( list_names[q] = name_mod) //here is where i am comparing
    {
    worker = name_mod;
    worker_num = (q + 1);
    }
    }
    }
    *************** *************** *************** *****
    heres the error...
    gaurd.cpp:71: error: could not convert
    `(+(&list_names )->std::vector<_T p, _Alloc>::operat or[] [with _Tp
    = std::string, _Alloc = std::allocator< std::string>](((unsigned
    int)q)))->std::basic_str ing<_CharT, _Traits, _Alloc>::operat or=
    [with _CharT = char, _Traits = std::char_trait s<char>, _Alloc
    = std::allocator< char>](((const std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char> >&)((const
    std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char>
    >*)(&name_mod)) ))' to `bool'


    Please help
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by shoes2908
    Hi, again I am in a rush and can't seem to remember how to search for a certain string in a string vector... heres my code:

    cin >> worker_num;
    if (cin.fail())
    {
    cin >> name_mod;
    for ( short q = 0; q < list_names.size (); q++)
    {
    if ( list_names[q] = name_mod) //here is where i am comparing
    {
    worker = name_mod;
    worker_num = (q + 1);
    }
    }
    }
    *************** *************** *************** *****
    heres the error...
    gaurd.cpp:71: error: could not convert
    `(+(&list_names )->std::vector<_T p, _Alloc>::operat or[] [with _Tp
    = std::string, _Alloc = std::allocator< std::string>](((unsigned
    int)q)))->std::basic_str ing<_CharT, _Traits, _Alloc>::operat or=
    [with _CharT = char, _Traits = std::char_trait s<char>, _Alloc
    = std::allocator< char>](((const std::basic_stri ng<char,
    std::char_trait s<char>, std::allocator< char> >&)((const
    std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char>
    >*)(&name_mod)) ))' to `bool'


    Please help

    Hi,
    The re is one issue
    [code=cpp]
    //if ( list_names[q] = name_mod) //here is where i am comparing
    //This shuld be like this
    if ( list_names[q] == name_mod) //here is where i am comparing

    [/code]

    I cant comment fully seeing only part of the code.
    Provide the code snippet fully that is the calling part and the function so i can check the logic.

    Raghuram

    Comment

    • shoes2908
      New Member
      • Dec 2007
      • 4

      #3
      Originally posted by gpraghuram
      Hi,
      The re is one issue
      [code=cpp]
      //if ( list_names[q] = name_mod) //here is where i am comparing
      //This shuld be like this
      if ( list_names[q] == name_mod) //here is where i am comparing

      [/code]

      I cant comment fully seeing only part of the code.
      Provide the code snippet fully that is the calling part and the function so i can check the logic.

      Raghuram

      wow, i am so dumb...... hahaha thank you

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        I'm sorry, but i thought for strings you can't use the == operator. I thought you had to use the strcmp function. I know with CStrings you can do the compare with the == operator. Have I been missing something all this time?!

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          Studlyami, you are familiar with C++ strings, right?

          Comment

          • Studlyami
            Recognized Expert Contributor
            • Sep 2007
            • 464

            #6
            I thought so, but apparently I'm an idiot. I some how had it engraved into my brain that you couldn't use the == operator with the standard strings. Now i tested it it works, then i tested using char * == char * and that works too! so i take it strcmp is from the C days and is there to support legacy code? Wow....that's really bad :(

            Comment

            • scruggsy
              New Member
              • Mar 2007
              • 147

              #7
              Originally posted by Studlyami
              I thought so, but apparently I'm an idiot. I some how had it engraved into my brain that you couldn't use the == operator with the standard strings. Now i tested it it works, then i tested using char * == char * and that works too! so i take it strcmp is from the C days and is there to support legacy code? Wow....that's really bad :(
              Not quite.
              == works for string objects.
              When you do char * == char * you're comparing two addresses, not two strings. Generally you want to compare the contents, not the addresses, which is what strcmp is for.

              Comment

              • Studlyami
                Recognized Expert Contributor
                • Sep 2007
                • 464

                #8
                I never noticed this before. In the sample program

                Code:
                    char *Testone = "test";
                    char *TestTwo = "test2";
                    char *TestSame = "test";
                
                    if(Testone == TestTwo)
                        cout<<"wrong"<<endl;
                    if(Testone == TestSame)
                        cout<<"right"<<endl;
                
                    TestTwo = "test";
                
                    if (TestTwo == Testone)
                    cout<<"right"<<endl;
                How does Testone and TestSame point to the same address? Then TestTwo receives the same address so how does the compiler link these char variables like that? is that the same behavior on other compilers?

                Comment

                • scruggsy
                  New Member
                  • Mar 2007
                  • 147

                  #9
                  Originally posted by Studlyami
                  I never noticed this before. In the sample program
                  How does Testone and TestSame point to the same address? Then TestTwo receives the same address so how does the compiler link these char variables like that? is that the same behavior on other compilers?
                  "test" and "test2" are string literals.
                  Your compiler is using string pooling to save space in the executable by storing each unique string literal only once. That's why testOne and testSame point to the same address. The C++ standard defines string literals as type const char*; however, for the sake of backwards compatibility with C they can be implicitly cast to char*. But to avoid undefined behavior you should always declare a pointer to a string literal as const (or, better, use a std::string).

                  Comment

                  • Studlyami
                    Recognized Expert Contributor
                    • Sep 2007
                    • 464

                    #10
                    Hey thanks for the information scruggsy that helped clear a few things up.

                    Comment

                    Working...