-
September 29th, 2014, 03:27 PM
#1
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".
-
September 29th, 2014, 03:45 PM
#2
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)
-
September 29th, 2014, 04:05 PM
#3
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??
-
September 29th, 2014, 04:29 PM
#4
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)
-
September 29th, 2014, 04:35 PM
#5
Re: Erase part of the string
This will not add 3 dots and .insert returns a string object. I tested it.
-
September 29th, 2014, 04:46 PM
#6
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)
-
September 29th, 2014, 05:00 PM
#7
Re: Erase part of the string
-
September 30th, 2014, 02:53 AM
#8
Re: Erase part of the string
what is the difference between size_t type and an int type?
-
September 30th, 2014, 04:19 AM
#9
Re: Erase part of the string
Originally Posted by flex567
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)
-
September 30th, 2014, 04:23 AM
#10
Re: Erase part of the string
Originally Posted by flex567
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)
-
September 30th, 2014, 04:44 AM
#11
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
-
September 30th, 2014, 04:49 AM
#12
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.
-
September 30th, 2014, 05:12 AM
#13
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
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)
-
September 30th, 2014, 05:22 AM
#14
Re: Erase part of the string
I edited both programs and still don't work.
-
September 30th, 2014, 05:24 AM
#15
Re: Erase part of the string
Originally Posted by flex567
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|