944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 751
  • C++ RSS
Oct 11th, 2009
0

Help with removing an element of a string :(

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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++)
  9. {
  10. if (setlist.at(i) == true)
  11. {
  12. cout << i;
  13. cout << s1;
  14. }
  15. }
  16. cout << "}";
  17. }


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.
Similar Threads
Reputation Points: 1
Solved Threads: 0
Light Poster
scantraXx- is offline Offline
27 posts
since Aug 2009
Oct 11th, 2009
0
Re: Help with removing an element of a string :(
Well at the end can't you just do an string erase on the last but one element?

string p = "{1,2,3,24,}";
p.erase(p.length()-2,1);
Last edited by iamthwee; Oct 11th, 2009 at 9:31 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 11th, 2009
0
Re: Help with removing an element of a string :(
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.
Last edited by scantraXx-; Oct 11th, 2009 at 9:40 am.
Reputation Points: 1
Solved Threads: 0
Light Poster
scantraXx- is offline Offline
27 posts
since Aug 2009
Oct 11th, 2009
-2
Re: Help with removing an element of a string :(
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();
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 11th, 2009
0
Re: Help with removing an element of a string :(
oops i meant

C++ Syntax (Toggle Plain Text)
  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.  
  8. string s1 = ",";
  9. cout << "{";
  10. for (int 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. cout << "}";
  26.  
  27. cin.get();
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 11th, 2009
0

How aboult?

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();
Reputation Points: 8
Solved Threads: 1
Newbie Poster
Poincarre is offline Offline
3 posts
since Oct 2009
Oct 11th, 2009
1
Re: Help with removing an element of a string :(
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.

C++ Syntax (Toggle Plain Text)
  1. void set::Display() const
  2. {
  3. cout << '{';
  4.  
  5. int i = 0;
  6. while(i < setlist.size() && setlist.at(i) != true)
  7. i++;
  8. if(i != setlist.size())
  9. cout << i;
  10. i++;
  11. for(i; i < setlist.size(); i++)
  12. if(setlist.at(i) == true)
  13. {
  14. cout << ", ";
  15. cout << i;
  16. }
  17.  
  18. cout << '}';
  19. }
Last edited by BeyondTheEye; Oct 11th, 2009 at 4:16 pm.
Reputation Points: 12
Solved Threads: 10
Junior Poster in Training
BeyondTheEye is offline Offline
63 posts
since Sep 2008
Oct 11th, 2009
0
Re: Help with removing an element of a string :(
Easy way out, just put a condition to check if "i" is at .size() - 1;
if not then print or else not print.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Oct 11th, 2009
0
Re: Help with removing an element of a string :(
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. }
Reputation Points: 8
Solved Threads: 1
Newbie Poster
Poincarre is offline Offline
3 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: [help] C++ algorithm
Next Thread in C++ Forum Timeline: layu po so much!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC