FAQ Database Discussion Community


String to Char Array not working

c++,arrays,string,char,std
I noticed that if my string is empty, the char array does not become empty either. See the below code: std::copy(strUsername.begin(), strUsername.end(), sRecord.m_strUsername); sRecord.m_strUsername is a char[1064], but if it is already populated and i try doing a copy (or strcpy) with strUsername as empty, nothing happens, even though I...

How can I make the sign appear at the end of the fill when using std::cout and std::stringstream?

c++,std,cout,stringstream
I have the following code for formatting a printout to always be 4 digits with sign included: std::stringstream pitch; pitch.precision(0); pitch.width(4); pitch.fill('0'); pitch << std::showpos << (int)(m_values["Pitch_1"]); I would also like to show the sign ("+"/"-"), but I want it to precede the fill, as follows: +002 However, the code...

Remove by iterator from std::vector

c++,vector,std
For removing an iterator from std::vector I can do these two things: std::vector<int>& vec = myNumbers; // use shorter name vec.erase(std::remove(vec.begin(), vec.end(), number_in), vec.end()); Or I can do this: auto it = find(vec.begin(), vec.end(), number_in); vec.erase(it); The second is more intuitive, I guess, but which one is faster? EDIT: Elements...

Can't verify the conversion from time_point to tm and tm back to time_point

c++,std,chrono,ctime
I created a current time_point and converted it to structure tm and printed it's values. Now converted this tm structure to time_point. On comparing the first and second time_points, it is telling that they're different. But the values of structure are exactly same. Can someone spot, where I'm doing wrong?...

What happens to an object instance after applying std::move

c++,std,move-semantics
I am trying to understand how std::move and rvalues work in C++ 11. I keep seeing the similar example in tutorials like this: Suppose we have this class : class Class { public: Class(Class &&b) { a = b.a; } int *a; } int main() { Class object1(5); Class object2(std::move(object1));...

E2316 'any_of' is not a member of 'std'

c++,std,c++builder,stl-algorithm,c++builder-6
So I'm trying to use std::any_of() function, but C++ Builder 6 says that there is an error: [C++ Error] Unit1.cpp(93): E2316 'any_of' is not a member of 'std' But I have #include <algorithm> in my Unit1.h #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include...

“cout << cout” - what does the output stand for?

c++,std
After a long day of coding i accidentaly wrote cout << "some text" << cout; instead of cout << "some text" << endl; Now it printed out a memory address. What does it point to ? ...

finding a memory allocation error with omnet++

c++,gdb,std,valgrind,omnet++
I am doing networks simulations under omnet++ & veins(v2.0-rc1), for this purpose I experiment 4 scenarios with 20 repetitions for both one. I am getting some errors (i guess that is a memory allocation error) during the simulations of the 3rd & 4th scenarios wich share a common functionnality (ack)....

How to use std::regex to find the next match in a string?

c++,regex,c++11,std
Trying to use std::regex in a scanner. So all it is supposed to do in my case is to find the first match starting at const char *p of the input sequence. It is not supposed to skip anything. It just needs to match as long as the expression is...

std::move to a function

c++,std,move
I have this example code: #include <iostream> class Struct { public: Struct() { std::cout << "0" << std::endl; } Struct(Struct&) { std::cout <<"1"<<std::endl;} Struct(const Struct&) { std::cout << "2" << std::endl; } }; class Struct2 { public: Struct s; Struct2() {} void setMember(const Struct& aux) { s = aux; }...

Stats properties among documents R

r,statistics,std,text-mining
I have a table, such as: test_data <- data.frame( doc = c(1,1,2,2,2,3,3), word = c("person", "grand", "person", "moment", "bout", "person", "moment"), frenq= c(9,8,5,4,3,5,3)) I would like to calculate mean and std for each "word" and create a new table such as. word freq (number of docs) mean std personn 19...

How efficient smart pointers are?

c++,pointers,c++11,std
I know, that std::shared_ptr uses reference counting, so it has copy&move semantics, on the other hand std::unique_ptr (hence the name unique) only has move semantics, so trying to copy it is a compile error. However, its not quite clear for me how big of a deal is that. Can I...

Printing out the hex vale of an unsigned char array in C++

c++,string,hex,std,cout
I want to print out the hex value of an unsigned char array using the cout function. The most obvious approach would be something like the following. unsigned char str[] = "foo bar baz\n"; for(unsigned short int i = 0; i < sizeof(str); i++){ std::cout << std::hex << str[i] <<...

need help to search element from vector of struct

c++,function,c++11,std
I have vector of struct partationPartItemTag_t type typedef struct partationPartItemTag { std::string partition; std::string partGroupId; std::string mathTuid; std::string partNumber; tag_t itemTag; }partationPartItemTag_t; I want to find element from vector having value for partition == 'a' and partGroupId == 'b' I have written following code and its working fine with C++11...

Split or trim string into one line

c++,string,std
I have a String which looks like this : string str = "1. Hallo \n2.Hello\n3.Bonjour\n4.Merhaba\n"; in the end I want to have sth like that: string str_line_3 = "3.Bonjour\n"; Im searching here for easy methods. To find the '\n' in a loop is to ugly imo ! Ideas ? edit:...

How do I write an int to a stringstream?

c++,std
I have std::stringstream with 1 byte: std::stringstream message; message.write((const char[]) {0x55}, 1); std::string res(message.str()); How append int a = 1; (4 bytes)? I want to get as values stored in the message's underlying std::string: 0x55 0x00 0x00 0x00 0x01 And right tool I chose for the job with the sequence...

c++ error: Function 'get_time' could not be resolved - Ubuntu g++

c++,ubuntu,std
I have the following piece of C++ code on my class, to convert a ISO 8601 string to a time_t structure: #include <iostream> #include <sstream> #include <string> #include <iomanip> #include <ctime> .... class code... struct tm tempTime; std::stringstream ss(data); ss >> std::get_time(&tempTime, "%FT%TZ"); std::time_t time = mktime(&tempTime); .... class code......

namespace std overloading less than

c++,c++11,tuples,std
I was curious about why this piece of code doesn't work: #include "stdafx.h" #include <iostream> #include <tuple> #include <string> #include <vector> #include <algorithm> typedef std::tuple<int, std::string> intString; bool operator<(intString& lhs, intString& rhs){ return std::tie(std::get<1>(lhs), std::get<0>(lhs)) < std::tie(std::get<1>(rhs), std::get<0>(rhs)); } void printIntStrings(std::vector<intString>& v){ for (intString& i : v){ std::cout <<...

Delete std::thread after calling join?

c++,multithreading,c++11,std,stdthread
I have some code that dynamically allocates a new std::thread from the C++11 <thread> header, like this: std::thread *th = new thread( /* my args */); Some time later, I call join: th->join(); Since I dynamically allocated the thread, do I also need to call delete th; to free the...

std::get for own class with tuple as member

c++,templates,std
I have a class like this: template<typename ... TTypes> class Composite { public: std::tuple<TTypes...> &getRefValues() { return values; } private: std::tuple<TTypes...> values; }; Can I define std::get for my class Composite? It should basically call the already defined std::get for the private tuple values. I was able to implement a...

Source code of std:: namespace

c++,std
I want to use the std::string namespace + member, but I do not want the size of my file to increase by the size of the #include <string> file. Is there anywhere that I can find the source code of the std:: namespace so I can just extract the string...

specific gnuplot by data grouping

data,gnuplot,std,mean
I'm new in gnuplot and sorry that my problem formulation might be unprecise, but I don't know how to find the tools/commnds needed to solve my problem. The code for plotting I would like to integrate in my bash file. I have a data set like: 285 1 50 7.35092...

std::hash specialization remains unused by std::unordered_map

c++,templates,c++11,std,unordered-map
I am trying to extend std::hash<T> by supplying a specialization for const char, so that I can use const char* as key type in std::unordered_map. This is what I tried: #include <unordered_map> #include <functional> #include <stdio.h> #include <string.h> #include <ctype.h> namespace std { template<> struct hash<const char*> { size_t operator()(const...

How to print a type vector> to screen c++?

c++,vector,tuples,std
Let's say I have std::vector<std::tuple<string ,int ,int>> tupleVector; tupleVector.push_back(std::tuple<string ,int ,int>("Joe", 2, 3)); tupleVector.push_back(std::tuple<string ,int ,int>("Bob", 4, 5)); How can I iterate on the vector to print all values of this vector containing a tuple?...

Why does unique_ptr::~unique_ptr need the definition of T?

c++,c++11,std,smart-pointers
If I have a class 'Bar': // bar.h class Bar { public: Bar() { } }; that I forward declare to use with an std::unique_ptr in another class 'Foo': // foo.h #include <memory> class Bar; class Foo { public: Foo(); private: std::unique_ptr<Bar> bar_; }; and whose definition I include in...

'for each' is not a member of std. C++11 support already enabled

c++,c++11,netbeans,std,codeblocks
The code from my professor's homework assignment is as follows. This is straight out of the box, I have not modified my professor's code. There is more to the program, but this is where the error happens. Problem line in bold. std::cout << "\nAll remaining courses with enrollments:\n"; allCourses =...

Sort structs depending on non-static value (C++)

c++,sorting,std
I have an EnemyRhombus class. It is a unit, which can move to different points on the map. I want to process points, to which it can move, in order of increasing distance to them. To do it I want to sort the points. Code 1: class EnemyRhombus { public:...

erase-remove idiom: did I just delete something?

c++,std,stdvector
I am using the erase-remove idiom: template <typename T> bool garbageCollectVector(std::vector<T>& v) { // Use the erase-remove idiom in combination with a lambda expression v.erase( std::remove_if(v.begin(), v.end(), [this](const T& elem) -> bool { return this->shouldRemove(elem); } ), v.end()); return /* what to return? */; } and want to return whether...

Using rand() to get a number but that number can't be the number that was last generated

c++,random,std
I want to use std::rand() to generate a number between 0 and amountOfNumbers, but the generated number can't be the same number that was last generated. I wrote this function: void reroll() { int newRand; while (true) { newRand = std::rand() % (amountOfNumbers); if (newRand == lastNumber) { continue; }...

C++ STL priority queue getting bad_alloc

c++11,std,priority-queue
I'm trying to use a STL priority queue, defined as: template <typename T> using min_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>; When running the following code: Label kNN(int k, const Matrix &trainingSet, const std::vector<Label> &trainingLabels, Matrix &evSet, int i1, const DistanceF &f) { Timer timer("kNN Timer"); min_queue<std::pair<double, Label>> distances; for (int i...

std::vector that holds class methods

c++,vector,enums,std,std-function
I have done this to enumerate my class members enum MemberType {A, B, C, D}; class Hello { public: std::vector<std::function<void(Hello*, void)>> m_members; void func() { }; Hello() { m_members[A] = func(); } }; What is wrong here?...

c++ argument of type x is incompatible with parameter of type with atexit

c++,std
I get this error when I try to run a function with theatexti() in std. I don't understand the error. Here is the error: IntelliSense: argument of type "void (Demo3Main::*)()" is incompatible with parameter of type "void (__cdecl *)()" and here is the code: Demo3Main::Demo3Main(void) : BaseEngine( 50 ) {...

Where does nullptr_t reside?

c++,c++11,std,nullptr
A bit of prehistory. I've been writing a game engine for quite some time. It's divided into several static libraries, like "utils", "rsbin" (resource system), "window", which are then linked into a single executable. It is a crossplatform engine, being compiled for Windows and for Android. Under Windows, I compile...

How to make function like operator