Questions on std::string

This is a discussion on Questions on std::string within the C++ Programming forums, part of the General Programming Boards category; I have two questions on std::strings, first of all, is there anyway to get the soucecode for string.cpp (or wherever ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Questions on std::string

    I have two questions on std::strings, first of all, is there anyway to get the soucecode for string.cpp (or wherever the class is implemented)? Or, is there a way to get a complete list of member functions in std::string?

    My second question is more complicated. I am trying to write a few functions that I didn't find in the partial list of std::string member functions that I've been looking at. I want my functions to take a reference to a string and operate on it, thereby not needing a return value and I can't seem to get it to work.

    Here is an example of one of the functions that I'm writing:
    Code:
    void StringToUpperCase(string* pString)
    {
    	for (int x = 0; x < *pString->length(); x++)
    	{
    		if ((((int)*pString[x] < 64) || ((int)*pString[x] > 122)) || (((int)*pString[x] >= 65) && ((int)*pString[x] <= 90))) {}
    		
    		*pString[x] -= 32; 
    	}
    }
    The function is being called like:
    Code:
    string p = "ilikestring";
    StringToUpperCase(&p);
    Now, it's entirely possible that i'm doing something really stupid, but I don't think I am, I think that there is some sort of different way to dereference strings (or maybe it's because it's a class) can anyone tell me how I can do this? It probably is something stupid, I heaven't touched a compiler in a long time...

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    I've found this to be a good reference for most of the c++ standard libray http://www.dinkumware.com/refxcpp.html


    For your code you have to dereference the pointer before using the array operator like this:
    Code:
    (*pString)[x]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is there anyway to get the soucecode for string.cpp (or wherever the class is implemented)?
    You shouldn't need to see it if you're playing nice.

    >can anyone tell me how I can do this?
    If you want to do it the handcrafted way, it would be better like this:
    Code:
    #include <cctype>
    
    void StringToUpperCase(string& str)
    {
      for (string::size_type i = 0; i < str.length(); i++)
        str[i] = toupper(static_cast<unsigned char>(str[i]));
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Hehe, thank you Sigfried, I tried to dereference but I didn't realize that I needed parenthesis(sp). Thanks again.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Or, is there a way to get a complete list of member functions in std::string?
    http://www.cppreference.com/cppstring.html
    Though that list seems to be missing the operator functions

    As for your function, why are you passing it as a pointer?
    Code:
    #include <cstring>
    #include <string>
    void StringToUpperCase(std::string &s)
    {
      for (int i=0; i < s.length() i++)
        s[i] = toupper(s[i]);
    }
    As a note you should never use the integer values when testing for characters. It is much better to use the character literals ('a', 'z', 'A', 'Z', etc)

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Hmmmph. I didn't realize that you could just declare in a function header that you only want a reference to a variable, I thought you had to declare it a pointer, pass in a reference, and dereference it in the function, thanks for pointing that out to me Thantos!

    As for your remark about not using numerals...why not? ASCII is standard isn't it?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Another way to do this just to expand your list of options (or just to overwhelm you) is the STL transform function along with the toupper function.

    Code:
    #include <algorithm>
    #include <string>
    #include <cctype>
    #include <iostream>
    
    int main()
    {
        std::string p = "ilikestring";
    
        std::cout << p << std::endl;
    
        std::transform(p.begin(),p.end(),p.begin(),toupper);
    
        std::cout << p << std::endl;
    
    }
    Output:

    Code:
    ilikestring
    ILIKESTRING
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I didn't realize that you could just declare in a function header that you only want a reference to a variable
    C++ is nice like that.

    >ASCII is standard isn't it?
    No. You'll see it most often, but it isn't everywhere. Therefore, if you want your code to be portable, you shouldn't make assumptions about the character set.
    My best code is written with the delete key.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is the STL transform function along with the toupper function.
    Be careful though. This has some obscure issues, and the example you provided is technically illegal because toupper is overloaded when cctype and iostream are both included. Therefore, it's ambiguous which toupper is meant.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    I have a different question about std::string now. Is it possible to make an array of dynamic size full of std::strings?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is it possible to make an array of dynamic size full of std::strings?
    Sure, though a std::vector would be easier to work with and safer.
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    ok, well how can I make a vector of strings? Can you give me a line of code that will do it?

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    well a vector of 5 ints would look like:
    Code:
    std::vector<int> vInts(5)

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can you give me a line of code that will do it?
    I can give you a few:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
      vector<string> v;
    
      v.push_back("A");
      v.push_back("B");
      v.push_back("C");
    
      vector<string>::const_iterator it = v.begin();
    
      while (it != v.end())
        cout<< *it++ <<endl;
    }
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    I'm trying to create a vector of strings that is uninitialized with a variable size, when I use your method Thantos, but replacing int with string, I get a bunch of wierd errors (i'm using VC++ 6.0)

    Prelude, what is the const_iterator in your example, do I need it to create a vector of strings? What does it do?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21