std

Why do I have to clear std::stringstream here?

孤街浪徒 提交于 2020-01-03 10:04:34
问题 I wrote a short testprogram to see if I can append to a string using stringstream repeatedly. In the first version I got Output1 and I don't really understand why s1 stays empty. I found out that I have to do ss.clear() and then get the expected result in Output2. Can anybody explain why it doesn't work without the clear? I would have expected that, If I repeatedly enter numbers and fetch them back into a string, I should get always the number. I was unsure if the number gets appended, but

error C2039: 'find' : is not a member of 'std'

家住魔仙堡 提交于 2020-01-03 07:25:09
问题 I just encountered a weird error which saying that find is not a member of std. error C2039: 'find' : is not a member of 'std' error C3861: 'find': identifier not found Basically, I want to find whether a string can be found in the vector Any idea why does this happen? the code assist tells me that there is find method in std. so this is basically what I did : #include "OperatorUtil.h" #include <iostream> #include <string> #include <stdlib.h> #include <math.h> #include <sstream> using

Does std::vector::resize() ever reallocate when new size is smaller than current size? [duplicate]

给你一囗甜甜゛ 提交于 2020-01-03 07:25:08
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: std::vector resize downward If I resize() an std::vector to some size smaller than its current size, is it possible that the vector will ever allocate new memory? This is important to me for performance reasons. 回答1: No, resize ing to a smaller size will never reallocate. In case the container shrinks, all iterators, pointers and references to elements that have not been removed remain valid after the resize and

A working std::copy example - printing an array

元气小坏坏 提交于 2020-01-03 04:05:07
问题 This does not work. I'm trying to learn how to use std::copy but I can't find any working code. I ran this in gcc 4.6.1. And it does not do anything when I hit control D. If I hit Control C...it prints out the new lines only. Found code here: Printing an array in C++? #include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() { std::vector<int> userInput; // Read until end of input. // Hit control D std::copy(std::istream_iterator<int>(std::cin), std::istream

Better way to convert a vector of uint8 to an ascii hexadecimal string

こ雲淡風輕ζ 提交于 2020-01-03 02:50:07
问题 I coded the following function to convert a std::vector of uint8_t to an ascii hexadecimal string (gnu++98 standard). ... string uint8_vector_to_hex_string(const vector<uint8_t>& v) { stringstream ss; vector<uint8_t>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { char hex_char[2]; sprintf(hex_char, "%x", *it); ss << "\\x" << hex_char; } return ss.str(); } ... It works fine. I was wondering if there is a better way to do this transformation, maybe using not both the

Is it possible to remove queue element by value?

爷,独闯天下 提交于 2020-01-02 03:17:26
问题 I want to remove element from queue with specific value. How to do such thing? (I am trying to create a concurrent mixture of map and queue and currently I try to implement on this answer) So I currently have such code: #ifndef CONCURRENT_QUEUED_MAP_H #define CONCURRENT_QUEUED_MAP_H #include <map> #include <deque> #include <boost/thread.hpp> #include <boost/thread/locks.hpp> template <class map_t_1, class map_t_2> class concurrent_queued_map { private: std::map<map_t_1, map_t_2> _ds; std:

Is it possible to remove queue element by value?

时间秒杀一切 提交于 2020-01-02 03:17:06
问题 I want to remove element from queue with specific value. How to do such thing? (I am trying to create a concurrent mixture of map and queue and currently I try to implement on this answer) So I currently have such code: #ifndef CONCURRENT_QUEUED_MAP_H #define CONCURRENT_QUEUED_MAP_H #include <map> #include <deque> #include <boost/thread.hpp> #include <boost/thread/locks.hpp> template <class map_t_1, class map_t_2> class concurrent_queued_map { private: std::map<map_t_1, map_t_2> _ds; std:

Pass std algos predicates by reference in C++

…衆ロ難τιáo~ 提交于 2020-01-02 01:24:08
问题 I am trying to remove elements from a std::list and keep some stats of deleted elements. In order to do so, I use the remove_if function from the list, and I have a predicate. I would like to use this predicate to gather statistics. Here is the code for the predicate: class TestPredicate { private: int limit_; public: int sum; int count; TestPredicate(int limit) : limit_(limit), sum(0), count(0) {} bool operator() (int value) { if (value >= limit_) { sum += value; ++count; // Part where I

Why does iostream include time.h?

感情迁移 提交于 2020-01-01 10:57:14
问题 Consider this code: #include <iostream> template<class C> struct time { }; int main() { } It produces (GCC 4.5): error: ‘template<class C> struct time’ redeclared as different kind of symbol /usr/include/time.h:186:15: error: previous declaration of ‘time_t time(time_t*)’ Why does iostream include time_t time(time_t*) ? Why does iostream include time_t time(time_t*) outside std namespace? (unanswered) Why, if I remove template<class C> , do I not get this error? 回答1: If you run g++ -H -Wall

std::wstring length

╄→尐↘猪︶ㄣ 提交于 2020-01-01 09:43:30
问题 What is the result of std::wstring.length() function, the length in wchar_t(s) or the length in symbols? And why? TCHAR r2[3]; r2[0] = 0xD834; // D834, DD1E - musical G clef r2[1] = 0xDD1E; // r2[2] = 0x0000; // '/0' std::wstring r = r2; std::cout << "capacity: " << r.capacity() << std::endl; std::cout << "length: " << r.length() << std::endl; std::cout << "size: " << r.size() << std::endl; std::cout << "max_size: " << r.max_size() << std::endl; Output> capacity: 351 length: 2 size: 2 max