AnsiString versus char *

This is a discussion on AnsiString versus char * within the C++ Programming forums, part of the General Programming Boards category; For example, there is no FindAndReplace method of std::string. You might want to use insert() or replace(), or erase(). Here ...

  1. #16
    Captain
    Guest
    For example, there is no FindAndReplace method of std::string. You might want to use insert() or replace(), or erase(). Here are the find operations:

    find() Finds the first occurance of a value
    rfind() Finds the last occurance of a value
    find_first_of()
    find_last_of()
    find_first_not_of()
    find_last_not_of()

    So I'm saying, that if you are going to compete with the STL than use a real example.

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Perhaps you could show me the equivalent std::string find_and_replace() implementation ?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #18
    Captain
    Guest
    As far as I can see there is not find_and_replace method. A std::string does support indexing with size_type indexes or iterators. If you want me to use what the std::string library has because you don't want to provide you String class details, than I can probably look it up and come up with something, but I'm leaving for work in 45 minutes and I'm currently busy reading about compiling multiple sources with gcc. I can probably come up with an algorithm tomorrow. In saying that, maybe one of the C++ algorithms support find and replace with a std::string argument. The C++ algorithms are not std::string methods however. I don't know, it's a big library.

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    As far as I can see there is not find_and_replace method...
    ...




    Code:
    
    
    class String {
    
     protected:
    
    char * memory;  //...I choose not to be const :)
    int _length;
    void Set(int this_length);
    
     public:
    
    
    friend ostream& operator << ( ostream& out, String& This);
    friend istream& operator >> ( istream& in, String& This );
    
    
    char * c_str();
    int length();
    
    char * Data();
    int Length();
    
    char& operator [] (int index);
    char * operator () (void);
    
    String();
    String(const char *str);
    String(const char ch);
    String(String& str);
    String(const string& str);
    ~String();
    
    void Resize(int new_length);
    void ResizeIf(int this_length_longer);
    
    /* the below are addidtionally defined for each of the following:
    std::string, char, and String...*/
    
    void Copy( const char *str );
    void Add( const char *str );
    char * Find( const char * str);
    int FindPosition( const char * str );
    char * FindNext( const char * str, char * next );
    int FindNextPosition( const char * str, char * next );
    char * Remove(const char * str );
    void FindAndReplace(const char * instance, const char * replace_with);
    char * operator -= (const char * str );
    void operator += (const char * str);
    void operator = (const char * str);
    bool operator == (const char * str);
    bool operator < (const char * str);
    bool operator << (const char * str);
    bool operator > (const char * str);
    bool operator <= (const char * str);
    bool operator >= (const char * str);
    bool operator != (const char * str);
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #20
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    God knows if there is an algorithm to do it. The closest I care to come up with is the replace() algorithm, but it only deals with characters. I don't know the stl well enough so I'll leave it at that.

    Code:
    #include<iostream>
    #include<string>
    #include<algorithm>
    
    using std::cout;
    using std::endl;
    using std::string;
    using std::replace;
    
    int main()
    {
      string text = ("A rose is a rose is a rose is a rose");
      cout << "Before: " << text << endl;
      replace(text.begin(), text.end(),'r', 't');
      cout << "After: " << text << endl;
      return 0;
    }

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Weird,

    replace(text.begin(), text.end(),'r', 't');

    Just hangs the program...

    Damn that STL
    (O.K., damn my ignorance...)

    ...will update...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #22
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    The first big difference between yours and the stl's string is that the latter is implemented as a template......therefore its not contained to type - char....

    This means I can use different types of strings for different situations.......the first that comes to mind is UNICODE

    Code:
    #define UNICODE
    #define _UNICODE
    #include <iostream>
    #include <string>
    #include <windows.h>
    #include <tchar.h>
    
    namespace tstring{
    #ifndef UNICODE
    	typedef std::basic_string<char> string;
    
    #else
    	typedef std::basic_string<WCHAR> string;
    #endif
    }
    
    int _tmain(){
    
    	tstring::string str = _T("Hello World");
    
    	MessageBox(NULL,str.c_str(),NULL,MB_OK);
    
    	return 0;
    }

  8. #23
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Thumbs up

    Oh, I know. My sole point was that I dislike it

    But yes, the STL is VERY generic, and it deserves credit for that...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #24
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    template <class elemType>
    class Mystring
    {
    ...
    };

    You could make your class generic and also create a hierarchy for specialized versions of your string class. I don't know if a string class is a great idea because the stl already provides one, but than again you might want some specialized functionality that the stl does not provide. I think that the stl is good, however i'm not used to working with it. Yet at the same time, I would like to know about the additions in the new 2003 standard C++ and be able to work with them right away in gcc. Very cool.

  10. #25
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Oh...and find & replace......

    I did this for someone a few days ago

    Code:
    #include <iostream>
    #include <string>
    
    
    int main(){
    
    using std::cout;
    using std::endl;
    using std::string;
    
    	string one("Hello World This Is Encoded");//string to encode
    	const string rep("%2B");//replacement token  
    	string::size_type indxD = 0;//return of find...
    
    	cout << "Unencoded " << one << endl;
    
    	while(1){
    		indxD = one.find_first_of (' ',0);//find a char....
    		if(indxD == string::npos)break;//if -1 then end (not found!)
    		else one.replace(indxD,1,rep.c_str(),rep.length());//replace with token
    	}
    
    	cout << "Encoded " << one << endl;
    
    
    }

  11. #26
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I didn't think that there was a one line solution, at least not with std::string.

  12. #27
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Fordy
    The first big difference between yours and the stl's string is that the latter is implemented as a template......therefore its not contained to type - char....
    templates don't make a class slower though. They only increase compilation time.

    And I guess the speed all depends on which compiler and STL you are using. My std::string implementation is fast and is reference counting, which saves a lot of time and memory.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #28
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Sang-drax
    templates don't make a class slower though. They only increase compilation time.

    And I guess the speed all depends on which compiler and STL you are using. My std::string implementation is fast and is reference counting, which saves a lot of time and memory.
    I know, I agree and I wasnt commending him for it....that's why I went on to show how a template implementation of a string can work in other situations other than ANSI...

    As I see it, I have a very fast, extensible, reliable, debugged, well documented implentation in the stl, so I can see no reason to build another string class.....and as for people trying to squeeze some insignificant bytes out of their exe.....well....enjoy .......as I see it, if your that concerened; go do it in ASM!!!!

  14. #29
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    .....and as for people trying to squeeze some insignificant bytes out of their exe.....well....enjoy .......as I see it, if your that concerened; go do it in ASM!!!!
    I'll take it that smartass remark was aimed at me, Fordy?


    It was well aimed
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #30
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Sebastiani
    I'll take it that smartass remark was aimed at me, Fordy?


    It was well aimed
    Nah...not really.....LOL

    Its just that I dont see the point in disregarding something like the STL in C++ for any code size increases.......

    C++ is there to make the coding experience better, quicker and less error prone....when you start stripping it away, and start concerning yourself with slight size increases and "cin is slower than scanf" then IMO its time to look at an alternative way of coding......sometimes the speed is nessasary (games etc...)....but for many things it isnt (How quick does text input need to be if the person using the app types with 1 finger!!??)

    I was reading an article recently where someone was using assembler to create the smallest PE exe file they could for a certain task......they removed the need for .DATA and .DATA? sections by basically having no variables apart from the stack and what they could jiggle around in the registers...all consts were in the code section and the code jumped over them......then they were looking at the switches for the linker to squeeze the section size down....then they were looking at ways of only loading the import libs that were absolutely nessasary....Interesting, but not a nice way to code

    Then I look at C++ and think, hell the size and efficiency of that isnt so worrying......hell its not as if we are using VB here is it

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 01:06 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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