10 Replies - 528 Views - Last Post: 27 January 2010 - 06:58 AM

#1 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Concatenate To String

Posted 21 January 2010 - 06:38 AM

Description: Place this into the file "strcat.hpp" and place that either in the current directory or in you include path. (or just paste the template function into your code)A small template to create an operator<< for the string class letting it act like a stringstream.
//-- Begin snippet: strcat.h ----------------
#ifndef STRCAT_HPP
#define STRCAT_HPP
#include <sstream>

template <class T>
std::string &operator<<(std::string &str, T obj) {
    std::ostringstream oss (ostringstream::out);
    oss << obj;
    str += oss.str();
    return str;
}
#endif
//-- End snippet: strcat.h ------------------

//What follows is a bit of test code...
#include <iostream>
#include "strcat.hpp" //You only need this if you put the above into its own header file.

using namespace std;

int main() {
    //declare a few variables to test concatenating
    int a = 100;
    double pi = 3.14159265;
    string clown = "Krusty";
    //declare our base string
    string message = "";
    //test adding some stuff to the string...
    // test cases: ((((str << char*) << int) << double) << string)
    message << "There are " << a << " reasons to learn that pi is " << pi << " says " << clown << " The Clown!";
    
    //Test out interaction with ostream &operator<<()
    cout << message << "!!!" << endl;
    
    //check to see if we accidentally concatenated anything...
    cout << """ << message << """ << endl;
    
    //output should be:
    //There are 100 reasons to learn that pi is 3.14159 says Krusty The Clown!!!!
    //"There are 100 reasons to learn that pi is 3.14159 says Krusty The Clown!"
    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Concatenate To String

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Concatenate To String

Posted 21 January 2010 - 06:38 AM

Description: Place this into the file "strcat.hpp" and place that either in the current directory or in you include path. (or just paste the template function into your code)A small template to create an operator for the string class letting it act like a stringstream.
//-- Begin snippet: strcat.h ----------------
#ifndef STRCAT_HPP
#define STRCAT_HPP
#include <sstream>

template <class T>
std::string &operator<<(std::string &str, T obj) {
    std::ostringstream oss (ostringstream::out);
    oss << obj;
    str += oss.str();
    return str;
}
#endif
//-- End snippet: strcat.h ------------------

//What follows is a bit of test code...
#include <iostream>
#include "strcat.hpp" //You only need this if you put the above into its own header file.

using namespace std;

int main() {
    //declare a few variables to test concatenating
    int a = 100;
    double pi = 3.14159265;
    string clown = "Krusty";
    //declare our base string
    string message = "";
    //test adding some stuff to the string...
    // test cases: ((((str << char*) << int) << double) << string)
    message << "There are " << a << " reasons to learn that pi is " << pi << " says " << clown << " The Clown!";
    
    //Test out interaction with ostream &operator<<()
    cout << message << "!!!" << endl;
    
    //check to see if we accidentally concatenated anything...
    cout << """ << message << """ << endl;
    
    //output should be:
    //There are 100 reasons to learn that pi is 3.14159 says Krusty The Clown!!!!
    //"There are 100 reasons to learn that pi is 3.14159 says Krusty The Clown!"
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Concatenate To String

Posted 21 January 2010 - 06:38 AM

Description: Place this into the file "strcat.hpp" and place that either in the current directory or in you include path. (or just paste the template function into your code)A small template to create an operator for the string class letting it act like a stringstream.
//-- Begin snippet: strcat.h ----------------
#ifndef STRCAT_HPP
#define STRCAT_HPP
#include <sstream>

template <class T>
std::string &operator<<(std::string &str, const T &obj) {
    std::ostringstream oss;
    oss << obj;
    str += oss.str();
    return str;
}
#endif
//-- End snippet: strcat.h ------------------

//What follows is a bit of test code...
#include <iostream>
#include "strcat.hpp" //You only need this if you put the above into its own header file.

using namespace std;

int main() {
    //declare a few variables to test concatenating
    int a = 100;
    double pi = 3.14159265;
    string clown = "Krusty";
    //declare our base string
    string message = "";
    //test adding some stuff to the string...
    // test cases: ((((str << char*) << int) << double) << string)
    message << "There are " << a << " reasons to learn that pi is " << pi << " says " << clown << " The Clown!";
    
    //Test out interaction with ostream &operator<<()
    cout << message << "!!!" << endl;
    
    //check to see if we accidentally concatenated anything...
    cout << """ << message << """ << endl;
    
    //output should be:
    //There are 100 reasons to learn that pi is 3.14159 says Krusty The Clown!!!!
    //"There are 100 reasons to learn that pi is 3.14159 says Krusty The Clown!"
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Concatenate To String

Posted 21 January 2010 - 06:38 AM

Description: Place this into the file "strcat.hpp" and place that either in the current directory or in you include path. (or just paste the template function into your code)A small template to create an operator for the string class letting it act like a stringstream.
//-- Begin snippet: strcat.hpp ----------------
#ifndef STRCAT_HPP
#define STRCAT_HPP
#include <sstream>

template <class T>
std::string &operator<<(std::string &str, const T &obj) {
    std::ostringstream oss;
    oss << obj;
    str += oss.str();
    return str;
}
#endif
//-- End snippet: strcat.hpp ------------------

//What follows is a bit of test code...
#include <iostream>
#include "strcat.hpp" //You only need this if you put the above into its own header file.

using namespace std;

int main() {
    //declare a few variables to test concatenating
    int a = 100;
    double pi = 3.14159265;
    string clown = "Krusty";
    //declare our base string
    string message = "";
    //test adding some stuff to the string...
    // test cases: ((((str << char*) << int) << double) << string)
    message << "There are " << a << " reasons to learn that pi is " << pi << " says " << clown << " The Clown!";
    
    //Test out interaction with ostream &operator<<()
    cout << message << "!!!" << endl;
    
    //check to see if we accidentally concatenated anything...
    cout << """ << message << """ << endl;
    
    //output should be:
    //There are 100 reasons to learn that pi is 3.14159 says Krusty The Clown!!!!
    //"There are 100 reasons to learn that pi is 3.14159 says Krusty The Clown!"
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Concatenate To String

Posted 26 January 2010 - 08:40 PM

An edited working version with typo's cleaned ... //-- Begin snippet: strcat.h ---------------- #ifndef STRCAT_HPP #define STRCAT_HPP #include template std::string& operator<<(std::string& str, const T& obj) { std::ostringstream oss; oss << obj; return str += oss.str(); } #endif //-- End snippet: strcat.h ------------------
Was This Post Helpful? 0
  • +
  • -

#6 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Concatenate To String

Posted 26 January 2010 - 08:46 PM

Opp's ... Edited snippet wouldn't fit ... anyways Nick ... a very nice snippet to have in one's C++ tool kit ... I'll try again to submit an edit: template std::string& operator<<(std::string& str, const T& obj) { std::ostringstream oss; oss << obj; return str += oss.str(); }
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Concatenate To String

Posted 26 January 2010 - 08:48 PM

I think you will need to PM me with your thoughts. I did note that this does not work on minGW as-is. Perhaps that is your point?
Was This Post Helpful? 0
  • +
  • -

#8 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Concatenate To String

Posted 26 January 2010 - 08:48 PM

std::string& operator<<(std::string& str, const T& obj) { std::ostringstream oss;
Was This Post Helpful? 0
  • +
  • -

#9 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Concatenate To String

Posted 26 January 2010 - 08:58 PM

PM sent re. typo and suggest pass obj in by const ref
Was This Post Helpful? 0
  • +
  • -

#10 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Concatenate To String

Posted 26 January 2010 - 09:12 PM

the const reference is a good point. I am not convinced this is a super great technique but I thought that it was interesting enough to share. I think that technically a stringstream is just the better option when you need this functionality.
Was This Post Helpful? 0
  • +
  • -

#11 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Concatenate To String

Posted 27 January 2010 - 06:58 AM

Your template ... will make getting a string just the way you'd like it ... a breeze. Thanks for sharing that very useful code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1