| | |
Help with removing an element of a string :(
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2009
Posts: 27
Reputation:
Solved Threads: 0
Hey guys.
This is really frustrating because it seems easy but I can't seem to find the correct function to remove this element of my string.
At the moment it will output something similar to this:
{1,2,3,} --> notice the , after the last digit?
It needs to be this:
{1,2,3} --> no ',' after the last digit.
Any help would be appreciated,
Regards.
This is really frustrating because it seems easy but I can't seem to find the correct function to remove this element of my string.
C++ Syntax (Toggle Plain Text)
void set::display() const // pre : none // post : displays the elements of this set enclosed in braces and // separated by commas eg {1,2,3} or {} (do not use backspace, delete etc) { string s1 = ","; cout << "{"; for (int i = 0; i < setlist.size(); i++) { if (setlist.at(i) == true) { cout << i; cout << s1; } } cout << "}"; }
At the moment it will output something similar to this:
{1,2,3,} --> notice the , after the last digit?
It needs to be this:
{1,2,3} --> no ',' after the last digit.
Any help would be appreciated,
Regards.
•
•
Join Date: Aug 2009
Posts: 27
Reputation:
Solved Threads: 0
0
#3 Oct 11th, 2009
That's true, but how will I be able to declare string p? Because there is a for loop present.. so it would have to store the output as a string somehow which I'm not too sure how to go about 
EDIT: The display() function is different every time because setlist is having elements added to it.. so the digits between the brackets will always be different.

EDIT: The display() function is different every time because setlist is having elements added to it.. so the digits between the brackets will always be different.
Last edited by scantraXx-; Oct 11th, 2009 at 9:40 am.
-2
#4 Oct 11th, 2009
I think your easiest answer would be
vector<int>setlist;
setlist.push_back(3);
setlist.push_back(12);
setlist.push_back(4);
string s1 = ",";
cout << "{";
for (int i = 0; i < setlist.size(); i++)
{
cout << setlist[i];
if (i!=setlist.size()-1)
{
cout << s1;
}
}
cout << "}";
cin.get();
vector<int>setlist;
setlist.push_back(3);
setlist.push_back(12);
setlist.push_back(4);
string s1 = ",";
cout << "{";
for (int i = 0; i < setlist.size(); i++)
{
cout << setlist[i];
if (i!=setlist.size()-1)
{
cout << s1;
}
}
cout << "}";
cin.get();
*Voted best profile in the world*
0
#5 Oct 11th, 2009
oops i meant
C++ Syntax (Toggle Plain Text)
vector<bool>setlist; setlist.push_back(true); setlist.push_back(false); setlist.push_back(true); setlist.push_back(true); string s1 = ","; cout << "{"; for (int i = 0; i < setlist.size(); i++) { if (setlist.at(i) == true) { cout << i; } if (i!=setlist.size()-1) { cout << s1; } } cout << "}"; cin.get();
*Voted best profile in the world*
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 1
1.
vector<bool>setlist;
2.
setlist.push_back(true);
3.
setlist.push_back(false);
4.
setlist.push_back(true);
5.
setlist.push_back(true);
6.
7.
int i
8.
string s1 = ",";
9.
cout << "{";
10.
for (i = 0; i < setlist.size(); i++)
11.
{
12.
13.
if (setlist.at(i) == true)
14.
{
15.
cout << i;
16.
17.
}
18.
19.
if (i!=setlist.size()-1)
20.
{
21.
22.
cout << s1;
23.
}
24.
}
25.
i--;
cout << "}";
26.
27.
cin.get();
vector<bool>setlist;
2.
setlist.push_back(true);
3.
setlist.push_back(false);
4.
setlist.push_back(true);
5.
setlist.push_back(true);
6.
7.
int i
8.
string s1 = ",";
9.
cout << "{";
10.
for (i = 0; i < setlist.size(); i++)
11.
{
12.
13.
if (setlist.at(i) == true)
14.
{
15.
cout << i;
16.
17.
}
18.
19.
if (i!=setlist.size()-1)
20.
{
21.
22.
cout << s1;
23.
}
24.
}
25.
i--;
cout << "}";
26.
27.
cin.get();
•
•
Join Date: Sep 2008
Posts: 55
Reputation:
Solved Threads: 9
1
#7 Oct 11th, 2009
I'd switch the order of printing around. First print a comma, then print the number.
This does require you to find the first number beforehand, though.
This does require you to find the first number beforehand, though.
C++ Syntax (Toggle Plain Text)
void set::Display() const { cout << '{'; int i = 0; while(i < setlist.size() && setlist.at(i) != true) i++; if(i != setlist.size()) cout << i; i++; for(i; i < setlist.size(); i++) if(setlist.at(i) == true) { cout << ", "; cout << i; } cout << '}'; }
Last edited by BeyondTheEye; Oct 11th, 2009 at 4:16 pm.
0
#8 Oct 11th, 2009
Easy way out, just put a condition to check if "i" is at .size() - 1;
if not then print or else not print.
if not then print or else not print.
I give up! 1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 1
0
#9 Oct 11th, 2009
1. void set::display() const
2. // pre : none
3. // post : displays the elements of this set enclosed in braces and
4. // separated by commas eg {1,2,3} or {} (do not use backspace, delete etc)
5. {
6. string s1 = "";
7. cout << "{";
8. for (int i = 0; i < setlist.size(); i++, s1 = ",")
9. {
10. if (setlist.at(i) == true)
11. {
12. cout << s1;
13. cout << i;
14. }
15. }
16. cout << "}";
17. }
2. // pre : none
3. // post : displays the elements of this set enclosed in braces and
4. // separated by commas eg {1,2,3} or {} (do not use backspace, delete etc)
5. {
6. string s1 = "";
7. cout << "{";
8. for (int i = 0; i < setlist.size(); i++, s1 = ",")
9. {
10. if (setlist.at(i) == true)
11. {
12. cout << s1;
13. cout << i;
14. }
15. }
16. cout << "}";
17. }
![]() |
Similar Threads
- Removing element from array (C++)
- Completely deleting a string array element : (C++)
- Removing chars from a string (C)
- removing spaces from a string (C++)
- Iterating and removing an element from ElementTree (Python)
- removing spaces from string (C++)
- Removing punctuation from a string (C++)
- Removing space from string (Shell Scripting)
Other Threads in the C++ Forum
- Previous Thread: [help] C++ algorithm
- Next Thread: layu po so much!
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






