1

I've been given a homework assignment to write a program in C++, but we're not allowed to use the string class. However, we are allowed to use the iostream library, including stringstream. I was thinking of using stringstream where I would have used string for building my classes, returning from functions, etc.

Does this sound like a good idea to you? Any pitfalls I should be aware of?

5
  • 2
    The only way to get stuff out of a stringstream is to convert it to a string. Commented Jun 1, 2009 at 7:05
  • This question is difficult to answer, since we don't know what was the reason you're not allowed to use the string class. Was the point of the assignment to find a clever way to use another standard class instead of the string? Or were you supposed to implement your own string class? Commented Jun 1, 2009 at 7:28
  • It's an assignment about polymorphism. We're building a shop database which stores a list of products and shopping baskets.They're just being stingy by not letting us use strings. I think they expect us to use char* or some other way of storing an array of chars. But we're free to figure out other creative solutions. Commented Jun 1, 2009 at 7:51
  • @workmad3 - Can't I send it directly to cout? or to a char or char[] or another stringstream? Commented Jun 1, 2009 at 7:52
  • Hopefully you only have to deal with ASCII characters. Think about wrapping vector<char> (or whatever you end up choosing) in your own string class Commented Jun 1, 2009 at 11:07

2 Answers 2

10

You could also use vector<char>. It's a good alternative to string. As Scott Meyers says in his "Effective STL" book:

Third, consider using a vector<char> instead of a string, vector implementations are not allowed to be reference counted, so hidden multithreading performance issues fail to arise. Of course, you forgo string's fancy member functions if you switch to vector<char>, but most of that functionality is available through STL algorithms anyway, so you're-not so much giving up functionality as you are trading one syntax for another.

I think the main problems that could arise from using stringstream are that it's not strictly a container. I'm not sure if it is possible to use algorithms and other regular STL stuff with streams.

Sign up to request clarification or add additional context in comments.

Comments

0

If the situation is a shop database without strings, you could just simply use an enum.

enum {
     Prod1,
     Prod2,
     Prod3
};

Which means you can just pass integers around as identifiers and then just have a print function with a switch statement.

void PrintProd(int product) {
    switch(product) {
         case Prod1: 
              cout << "Product 1" << endl;
              break;
         case Prod2:
              cout << "Product 2" << endl;
              break;
     // etc.
     }
}

This allows you to bypass a lot of trouble. If you don't mind dealing with the heap, a vector of char* allocating the product names with the id's being the index into the vector would work too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.