Custom Data Types with String Streams

Can I use string streams with custom data types?

Yes, you can use string streams with custom data types in C++. To do this, you need to overload the insertion (<<) and extraction (>>) operators for your custom types.

This allows your data types to be inserted into and extracted from string streams just like built-in types.

Insertion Operator

To insert a custom data type into a string stream, overload the << operator. Here's an example with a custom Person class:

#include <iostream>
#include <sstream>

class Person {
 public:
  Person(const std::string& name, int age)
    : Name{name}, Age{age} {}

  friend std::ostream& operator<<(
    std::ostream& os, const Person& person);

 private:
  std::string Name;
  int Age;
};

std::ostream& operator<<(
  std::ostream& os, const Person& person
) {
  os << "Name: " << person.Name
     << ", Age: " << person.Age;

  return os;
}

int main() {
  Person p{"Alice", 30};
  std::ostringstream Stream;
  Stream << p;  
  std::cout << Stream.str();
}
Name: Alice, Age: 30

Extraction Operator

To extract a custom data type from a string stream, overload the >> operator. Here's an example:

#include <iostream>
#include <sstream>

class Person {
 public:
  Person() : Name{""}, Age{0} {}

  friend std::istream& operator>>(
    std::istream& is, Person& person);

  std::string Name;
  int Age;
};

std::istream& operator>>(
  std::istream& is, Person& person
) {
  is >> person.Name >> person.Age;
  return is;
}

int main() {
  std::istringstream Stream{"Bob 25"};
  Person p;
  Stream >> p;  
  std::cout << "Extracted Person - Name: "
    << p.Name << ", Age: " << p.Age;
}
Extracted Person - Name: Bob, Age: 25

Practical Example

Combining both insertion and extraction operators, you can serialize and deserialize your custom types easily:

#include <iostream>
#include <sstream>

class Person {
 public:
  Person() : Name{""}, Age{0} {}
  Person(const std::string& name, int age)
    : Name{name}, Age{age} {}

  friend std::ostream& operator<<(
    std::ostream& os, const Person& person
  );

  friend std::istream& operator>>(
    std::istream& is, Person& person
  );

  std::string Name;
  int Age;
};

std::ostream& operator<<(
  std::ostream& os, const Person& person
) {
  os << person.Name << " " << person.Age;
  return os;
}

std::istream& operator>>(
  std::istream& is, Person& person
) {
  is >> person.Name >> person.Age;
  return is;
}

int main() {
  Person p1{"Charlie", 40};
  std::ostringstream outStream;
  outStream << p1;

  std::istringstream inStream(outStream.str());
  Person p2;
  inStream >> p2;

  std::cout << "Serialized: "
    << outStream.str() << "\n";
  std::cout << "Deserialized - Name: "
    << p2.Name << ", Age: " << p2.Age;
}
Serialized: Charlie 40
Deserialized - Name: Charlie, Age: 40

Summary

  • Overloading Operators: Overload << for insertion and >> for extraction.
  • Serialization/Deserialization: Use string streams to easily serialize and deserialize custom data types.
  • Practical Usage: Enable your custom types to work seamlessly with string streams.

By overloading the appropriate operators, you can integrate custom data types with string streams effectively, making your code more flexible and powerful.

String Streams

A detailed guide to C++ String Streams using std::stringstream. Covers basic use cases, stream position seeking, and open modes.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Resetting a String Stream
How do I reset the content of a string stream?
Converting String Stream to String
How do I convert a string stream to a std::string?
String Stream Performance
Is using string streams more efficient than concatenating strings directly?
Clearing a String Stream
How do I clear a string stream in C++?
Parsing CSV with String Streams
Can I use string streams to parse comma-separated values (CSV)?
String Streams and Multithreading
Can I use string streams in multithreaded applications?
Checking String Stream Success
How do I check if a string stream operation was successful?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant