CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Erase part of the string

    Code:
    // string::erase
    #include <iostream>
    #include <string>
    
    int main ()
    {
      std::string str ("This is an example sentence.");
      std::cout << str << '\n';
                                               
      str.erase (10,8);                       
      std::cout << str << '\n';
    Code:
    Output:
    This is an sentence.
    I don't understand this program. What do these 2 numbers represent? The index of an element in a character array? The 10th character of the array is 'n' and 8th is a "space".

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Erase part of the string

    The first number is the index value into the string for the first char to be erased - which starts at 0 not 1! The second is the number of chars to be erased from this position. See http://www.cplusplus.com/reference/string/string/erase/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Erase part of the string

    oh, i see.

    what about this

    Code:
        string s1 = "***carr***";
        string s2;
    
        s2 = s1.insert(s1.end(),3,'.');
    Why is this not the way to add 3 dots to the end of the string??

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Erase part of the string

    Code:
    s1.insert(s1.end(),3,'.');
    This will indeed add 3 dots to the end of the string s1. But .insert doesn't return an object that can be assigned to a type string. See See http://www.cplusplus.com/reference/s...string/insert/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Erase part of the string

    This will not add 3 dots and .insert returns a string object. I tested it.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Erase part of the string

    Under VS2013

    Code:
    int main()
    {
    string s1 = "***carr***";
    
    	cout << s1 << endl;
    
    	s1.insert(s1.end(), 3, '.');
    
    	 cout << s1 << endl;
    
    	 return 0;
    }
    produces the output
    Code:
    ***carr***
    ***carr***...
    What compiler are you using?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Erase part of the string

    miniGw

  8. #8
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Erase part of the string

    what is the difference between size_t type and an int type?

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Erase part of the string

    Quote Originally Posted by flex567 View Post
    This will not add 3 dots and .insert returns a string object. I tested it.
    In c++11, when the first argument to .insert is an iterator, then .insert() returns an iterator (except when using an initializer list). This is slightly different in c++98. See http://www.cplusplus.com/reference/s...string/insert/ for further details.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Erase part of the string

    Quote Originally Posted by flex567 View Post
    what is the difference between size_t type and an int type?
    See http://stackoverflow.com/questions/1...-int-vs-size-t
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Erase part of the string

    Why this program won't compile?
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    bool isPalindrom(string s){
        string temp;
        int j = s.size()-1;
    
        for(int i = 0; i < s.size();i++){
    
            temp[i] = s[j];
            j--;
    
        }
        cout << temp << endl;
    
        return true;
    
    }
    
    
    int main{
    
    string s1("ana")
    isPalindrom(s1);
    return 0;}
    Last edited by flex567; September 30th, 2014 at 05:21 AM. Reason: edit

  12. #12
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Erase part of the string

    This one will compile but will produce strange results?
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main{
        
        string s3 = "Ana";
        string ss;
       
    
        ss.insert(0,s3[0],1);
    
    return 0;}
    Last edited by flex567; September 30th, 2014 at 05:21 AM.

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Erase part of the string

    As you are not referencing the stl classes by their namespace, you need
    Code:
    using namespace std;
    after the includes - or use
    Code:
    std::string ss;
    etc

    You can't have nested functions. main hasn't been given a parameter list. This will compile
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    bool isPalindrom(string s){
    		string temp;
    		int j = s.size() - 1;
    
    		for (int i = 0; i < s.size(); i++){
    
    			temp[i] = s[j];
    			j--;
    
    		}
    		cout << temp << endl;
    
    		return true;
    
    	}
    
    int main()
    {
    	isPalindrom("qweewq");
    
    	return 0;
    }
    Last edited by 2kaud; September 30th, 2014 at 05:16 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Apr 2012
    Location
    Slovenia
    Posts
    259

    Re: Erase part of the string

    I edited both programs and still don't work.

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Erase part of the string

    Quote Originally Posted by flex567 View Post
    This one will compile but will produce strange results?
    I think you mean
    Code:
    	ss.insert(0, 1, s3[0]);
    1st param is position, 2nd is number to insert and 3rd is character to insert. See http://www.cplusplus.com/reference/s...string/insert/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured