stl

std regex_search to match only current line

元气小坏坏 提交于 2019-12-31 04:10:24
问题 I use a various regexes to parse a C source file, line by line. First i read all the content of file in a string: ifstream file_stream("commented.cpp",ifstream::binary); std::string txt((std::istreambuf_iterator<char>(file_stream)), std::istreambuf_iterator<char>()); Then i use a set of regex, which should be applied continusly until the match found, here i will give only one for example: vector<regex> rules = { regex("^//[^\n]*$") }; char * search =(char*)txt.c_str(); int position = 0,

STL not release memory from System level

僤鯓⒐⒋嵵緔 提交于 2019-12-31 03:42:13
问题 As I know STL has automatic memory management. But when I use something like top or ps -aux to show the memory usage of a process, it shows even the STL object are out of scope, these memory is still possessed by process. Here is an example: void run() { map<int, int> a; for(int i = 0; i < 1000000; i++) { a[i] = i; } // 64376K memory used by process } int main() { run(); sleep(5); // still 64376 memory used map<int, int> a; for(int i = 0; i < 1000000; i++) { a[i] = i; } // still 64376 memory

Is there an STL container that stores an array of elements in contiguous memory where the element size is specified at runtime?

非 Y 不嫁゛ 提交于 2019-12-31 03:11:13
问题 I'm trying to create container that looks close to how my file spec works. It's like a vector but the type of the elements is defined by a hashtable. If I knew the type at compile-time I could just write something like this: struct foo { float a,b,c; int d; byte e,f; }; std::vector<foo> foovector; foovector.push_back(foo f); I don't have the struct at compile time. I only have a schema I get from the file header. All elements are the same size and have the same offsets for each item inside an

Calling a member function pointer stored in a std map

夙愿已清 提交于 2019-12-31 03:08:16
问题 I'm storing a map in a class that has strings as keys and pointers to member functions as values. I'm having trouble calling the right function throw the function pointer. Here is the code: #include <iostream> #include <string> #include <map> using namespace std; class Preprocessor; typedef void (Preprocessor::*function)(); class Preprocessor { public: Preprocessor(); ~Preprocessor(); void processing(const string before_processing); private: void take_new_key(); map<string, function> srch

Why overloaded ' operator < ' should be const for class?

我是研究僧i 提交于 2019-12-31 02:15:14
问题 Can anybody explain this behavior in context of STL sort algorithm? If operator < is not defined const it gives error, error: passing ‘const B’ as ‘this’ argument of ‘bool B::operator<(const B&)’ discards qualifiers [-fpermissive] while (__pivot < *__last) Is sort algo lhs const object or sort is const method? class B { public: ... bool operator < (const B& b) const // why const required here? { return (m_i < b.m_i); } ... private: int m_i; int m_j; }; int main() { vector<B> Bvec2 {B(5), B(3)

Using “unique()” on a vector of vectors in C++

半城伤御伤魂 提交于 2019-12-31 01:28:06
问题 I hope this is not a duplicate question, but if it is, feel free to point me in the right direction. I have a vector<vector<int> > . Is it possible to use unique() on this? Something like: vector<vector<int> > myvec; //blah blah do something to myvec vector<vector<int> >::interator it = unique(myvec.begin(), myvec.end()); Would the range myvec.begin() to it be unique? 回答1: Yes, as long as your vector is sorted. See unique () STL documentation for details. Here is an example of usage: #include

How to create a container of noncopyable elements

核能气质少年 提交于 2019-12-30 18:34:50
问题 Is there a way use STL containters with non-copyable elements? something like this: class noncopyable { noncopyable(noncopyable&); const noncopyable& operator=(noncopyable&); public: noncopyable(){}; }; int main() { list<noncopyable> MyList; //error C2248: 'noncopyable::noncopyable' : cannot access private member declared in class 'noncopyable' } 回答1: No, non-copyable elements can't be in C++ container classes. According to the standard, 23.1 paragraph 3, "The type of objects stored in these

Do stl containers use implicit sharing?

試著忘記壹切 提交于 2019-12-30 18:27:46
问题 Its known that Qt widgets use implicit sharing. So I am interested if stl containers std::vector , std::string use implicit sharing too. If no, why? Since it is very useful. And if the answer is yes, how we can ascertain in it? I need simple C++ stl program which shows that stl containers use implicit sharing. It doesn't do deep copy when is copied. 回答1: No. They cannot. When you try to modify the contents of the container, or even calling a mutable begin() on it, it would imply a potential

Do stl containers use implicit sharing?

百般思念 提交于 2019-12-30 18:27:00
问题 Its known that Qt widgets use implicit sharing. So I am interested if stl containers std::vector , std::string use implicit sharing too. If no, why? Since it is very useful. And if the answer is yes, how we can ascertain in it? I need simple C++ stl program which shows that stl containers use implicit sharing. It doesn't do deep copy when is copied. 回答1: No. They cannot. When you try to modify the contents of the container, or even calling a mutable begin() on it, it would imply a potential

how to find duplicates in std::vector<string> and return a list of them?

走远了吗. 提交于 2019-12-30 17:27:07
问题 So if I have a vector of words like: Vec1 = "words", "words", "are", "fun", "fun" resulting list: "fun", "words" I am trying to determine which words are duplicated, and return an alphabetized vector of 1 copy of them. My problem is that I don't even know where to start, the only thing close to it I found was std::unique_copy which doesn't exactly do what I need. And specifically, I am inputting a std::vector<std::string> but outputting a std::list<std::string> . And if needed, I can use