I've been reading Accelerated C++ and I have to say it's an interesting book.
In chapter 6, I have to use a function from <algorithm> to concatenate from a vector<string> into a single string. I could use accumulate, but it doesn't help because string containers can only push_back characters.
int main () {
using namespace std;
st...
Hi,
I'm not very familiar with locale-specific conversions so I may be using the wrong terminology here. This is what I want to have happen.
I want to write a function
std::string changeLocale( const std::string& str, const std::locale& loc )
such that if I call this function as follows:
changeLocale( std::string( "1.01" ), std::lo...
Using boost::mpl, I can create a typedef of a three element vector like follows:
typedef boost::mpl::vector_c<int,1,2,3> height_t;
I can pull the values out of this typedef with the following snippit:
std::vector<int> height;
boost::mpl::for_each<height_t>(boost::bind(&std::vector<int>::push_back, &height, _1));
assert(height[0] ...
I have to port a C++ STL application to Python. I am a Python newbie, but have been programming for over a decade. I have a great deal of experience with the STL, and find that it keeps me hooked to using C++. I have been searching for the following items on Google these past several days:
Python STL (in hope of leveraging my years of ...
I've got various std::vector instances with numeric data in them, primarily int16_t, int32_t, etc. I'd like to dump this data to a file in as fast a manner as possible. If I use an ostream_iterator, will it write the entire block of memory in a single operation, or will it iterate over the elements of the vector, issuing a write opera...
Browsing through some C++ questions I have often seen comments that a STL-friendly class should implement a swap function (usually as a friend.) Can someone explain what benefits this brings, how the STL fits into this and why this function should be implemented as a friend?
...
I have the following code that uses a for loop and I would like to use transform, or at least for_each instead, but I can't see how.
typedef std::list<boost::function<void(void) > CallbackList;
CallbackList callbacks_;
//...
for(OptionsMap::const_iterator itr = options.begin(); itr != options.end(); ++itr)
{
callbacks_.push_back(boo...
I tried to add objects to the "content" vector, and use show() on all of them.
But the objects that are children (A, B) of "Base" behave like they were of "Base" type,
what is not my intention. As it seems, I tried to use virtual functions but it doesn't work.
I hope that the code will speak for itself.
class Base {
public:
...
Suppose I have a struct containing a std::string, like this:
struct userdata{
int uid;
std::string username;
}
Do I need to create a copy ctor or anything to return it from a function or to use it inside a STL container? Consider this function:
userdata SomeClass::GetUserData(unsigned int uid)
{
//do error che...
Hi,
I've been trying to learn more about private inheritance and decided to create a string_t class that inherits from std::basic_string. I know a lot of you will tell me inheriting from STL classes is a bad idea and that it's better to just create global functions that accept references to instances of these classes if I want to extend ...
I am maintaining a project that can take a considerable time to build so am trying to reduce dependencies where possible. Some of the classes could make use if the pImpl idiom and I want to make sure I do this correctly and that the classes will play nicely with the STL (especially containers.) Here is a sample of what I plan to do - d...
Hello
Is there a way to enforce STL container alignment to specific byte, using attribute((aligned))perhaps? the target compilers are not Microsoft Visual C++.
What libraries, if any, provide specialized templates of STL algorithms which have specific explicit vectorization, e.g. SSE. My compilers of interest are g++, Intel, and IBM X...
I have a class that adapts std::vector to model a container of domain-specific objects. I want to expose most of the std::vector API to the user, so that he/she may use familiar methods (size, clear, at, etc...) and standard algorithms on the container. This seems to be a reoccurring pattern for me in my designs:
class MyContainer : pub...
I've always wanted a bit more functionality in STL's string. Since subclassing STL types is a no no, mostly I've seen the recommended method of extension of these classes is just to write functions (not member functions) that take the type as the first argument.
I've never been thrilled with this solution. For one, it's not necessarily ...
Hi,
I've recently started using boost lambda and thought I'd try and use it in places where it will/should make things easier to read.
I have some code similar to the following
std::vector< X * > v;
for ( int i = 0 ; i < 20 ; ++i )
v.push_back( new X() );
and later on, to delete it...
std::for_each( v.begin(), v.end(), boost::l...
Are there any advantages of std::for_eachover for loop? To me, std::for_each only seems to hinder the readability of code. Why do then some coding standards recommend its use?
...
I just read the code for std::for_each:
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
return f;
}
and could not see any good reasons for this template function to return the input function. Does anyone have any exampl...
I want to use the STL's Map container to lookup a pointer by using binary data as a key so I wrote this custom function object:
struct my_cmp
{
bool operator() (unsigned char * const &a, unsigned char * const &b)
{
return (memcmp(a,b,4)<0) ? true : false;
}
};
And using it like this:
map<unsigned char *, void *,...
There are a number of Win32 functions that take the address of a buffer, such as TCHAR[256], and write some data to that buffer. It may be less than the size of the buffer or it may be the entire buffer.
Often you'll call this in a loop, for example to read data off a stream or pipe. In the end I would like to efficiently return a strin...
std::exception class is defined as follows
exception() throw() { }
virtual ~exception() throw();
virtual const char* what() const throw();
what does the throw() syntax mean in a declaration?
Can throw() take parameters? What does no parameters mean?
...