Skip to main content
added 79 characters in body
Source Link
David G
  • 97.6k
  • 41
  • 173
  • 258

Here is an implementation of how to append an int to a string using the parsing and formatting facets from the IOStreams library.

#include <iostream>
#include <locale>
#include <string>
 
template <class Facet>
struct erasable_facet : Facet
{
    erasable_facet() : Facet(1) { }
    ~erasable_facet() { }
};
 
void append_int(std::string& s, int n)
{
    erasable_facet<std::num_put<char,
                                std::back_insert_iterator<std::string>>> facet;
    std::ios str(nullptr);
    
    facet.put(std::back_inserter(s), str,
                                     str.fill(), static_cast<unsigned long>(n));
}
 
int main()
{
    std::string str = "ID: ";
    int id = 123;
    
    append_int(str, id);
    
    std::cout << str; // ID: 123
}

Here is an implementation of how to append an int to a string using the parsing and formatting facets from the IOStreams library.

#include <iostream>
#include <locale>
#include <string>
 
template <class Facet>
struct erasable_facet : Facet
{
    erasable_facet() : Facet(1) { }
    ~erasable_facet() { }
};
 
void append_int(std::string& s, int n)
{
    erasable_facet<std::num_put<char, std::back_insert_iterator<std::string>>> facet;
    std::ios str(nullptr);
    
    facet.put(std::back_inserter(s), str, str.fill(), static_cast<unsigned long>(n));
}
 
int main()
{
    std::string str = "ID: ";
    int id = 123;
    
    append_int(str, id);
    
    std::cout << str; // ID: 123
}

Here is an implementation of how to append an int to a string using the parsing and formatting facets from the IOStreams library.

#include <iostream>
#include <locale>
#include <string>
 
template <class Facet>
struct erasable_facet : Facet
{
    erasable_facet() : Facet(1) { }
    ~erasable_facet() { }
};
 
void append_int(std::string& s, int n)
{
    erasable_facet<std::num_put<char,
                                std::back_insert_iterator<std::string>>> facet;
    std::ios str(nullptr);
    
    facet.put(std::back_inserter(s), str,
                                     str.fill(), static_cast<unsigned long>(n));
}
 
int main()
{
    std::string str = "ID: ";
    int id = 123;
    
    append_int(str, id);
    
    std::cout << str; // ID: 123
}
Source Link
David G
  • 97.6k
  • 41
  • 173
  • 258

Here is an implementation of how to append an int to a string using the parsing and formatting facets from the IOStreams library.

#include <iostream>
#include <locale>
#include <string>
 
template <class Facet>
struct erasable_facet : Facet
{
    erasable_facet() : Facet(1) { }
    ~erasable_facet() { }
};
 
void append_int(std::string& s, int n)
{
    erasable_facet<std::num_put<char, std::back_insert_iterator<std::string>>> facet;
    std::ios str(nullptr);
    
    facet.put(std::back_inserter(s), str, str.fill(), static_cast<unsigned long>(n));
}
 
int main()
{
    std::string str = "ID: ";
    int id = 123;
    
    append_int(str, id);
    
    std::cout << str; // ID: 123
}