stl

Replace line breaks in a STL string

烈酒焚心 提交于 2019-12-22 04:04:29
问题 How can I replace \r\n in an std::string ? 回答1: Use this : while ( str.find ("\r\n") != string::npos ) { str.erase ( str.find ("\r\n"), 2 ); } more efficient form is : string::size_type pos = 0; // Must initialize while ( ( pos = str.find ("\r\n",pos) ) != string::npos ) { str.erase ( pos, 2 ); } 回答2: don't reinvent the wheel, Boost String Algorithms is a header only library and I'm reasonably certain that it works everywhere. If you think the accepted answer code is better because its been

Find the first element strictly less than a key in a vector sorted in descending order

雨燕双飞 提交于 2019-12-22 03:55:36
问题 I understand that this task can be accomplished using the find_if() STL-Algorithm function as follows: long long int k; //k = key scanf("%lld",&k); auto it = find_if(begin(v),end(v),[k](auto e){return e<k;}); However I require the result to be obtained in logarithmic time. Since the vector is already sorted in descending order I'd like to use a binary search approach. I understand the STL Algorithm function lower_bound and upper_bound guarantee a logarithmic complexity. However I'm unable to

What is the difference between “UTF-16” and “std::wstring”?

帅比萌擦擦* 提交于 2019-12-22 03:47:32
问题 Is there any difference between these two string storage formats? 回答1: std::wstring is a container of wchar_t . The size of wchar_t is not specified—Windows compilers tend to use a 16-bit type, Unix compilers a 32-bit type. UTF-16 is a way of encoding sequences of Unicode code points in sequences of 16-bit integers. Using Visual Studio, if you use wide character literals (e.g. L"Hello World" ) that contain no characters outside of the BMP, you'll end up with UTF-16, but mostly the two

read huge text file line by line in C++ with buffering

蹲街弑〆低调 提交于 2019-12-22 03:41:39
问题 I need to read huge 35G file from disc line by line in C++. Currently I do it the following way: ifstream infile("myfile.txt"); string line; while (true) { if (!getline(infile, line)) break; long linepos = infile.tellg(); process(line,linepos); } But it gives me about 2MB/sec performance, though file manager copies the file with 100Mb/s speed. I guess that getline() is not doing buffering correctly. Please propose some sort of buffered line-by-line reading approach. UPD: process() is not a

std::iterator, pointers and VC++ warning C4996

吃可爱长大的小学妹 提交于 2019-12-22 03:30:09
问题 int *arr = (int*) malloc(100*sizeof(int)); int *arr_copy = (int*) malloc(100*sizeof(int)); srand(123456789L); for( int i = 0; i < 100; i++) { arr[i] = rand(); arr_copy[i] = arr[i]; } // ------ do stuff with arr ------ // reset arr... std::copy(arr_copy, arr_copy+100, arr); while compiling this I get this warning for std::copy() : c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2227): warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe -

Prettier syntax for “pointer to last element”, std::vector?

泄露秘密 提交于 2019-12-22 03:28:06
问题 I'm wondering if there is prettier syntax for this to get a normal pointer (not an iterator) to the last element in a C++ vector std::vector<int> vec; int* ptrToLastOne = &(*(vec.end() - 1)) ; // the other way I could see was int* ptrToLastOne2 = &vec[ vec.size()-1 ] ; But these are both not very nice looking! 回答1: int* ptrToLastOne = &vec.back(); // precondition: !vec.empty() 回答2: int* ptrToLast = &(vec.back()); // Assuming the vector is not empty. 回答3: Some more options: int* ptrToLast = &

Detailed difference between functor's call and function call?

本秂侑毒 提交于 2019-12-22 02:30:08
问题 The key reason this works is that for_each () doesn’t actually assume its third argument to be a function. It simply assumes that its third argument is something that can be called with an appropriate argument. A suitably defined object serves as well as – and often better than – a function. For example, it is easier to inline the application operator of a class than to inline a function passed as a pointer to function. Consequently, function objects often execute faster than do ordinary

C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用

主宰稳场 提交于 2019-12-22 02:11:10
一.find运算 假设有一个int型的vector对象,名为vec,我们想知道其中 是否包含某个特定值 。 解决这个问题最简单的方法时使用标准库提供的find运算: 1 // value we'll look for 2 int search_value = 42; 3 4 //call find to see if that value is present 5 vector<int>::const_iterator result = find(vec.begin() , vec.end() , search_value); 6 7 //report the result 8 cout<<"The value "<<search_value 9 <<(result == vec.end() ? " is not present" : "is present") 10 <<endl; 具体实现代码: 1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 6 int main() 7 { 8 // value we'll look for 9 int search_value = 42; 10 int ival; 11 vector<int> vec; 12 13

Idiomatic C++ for reading from a const map

房东的猫 提交于 2019-12-22 01:53:28
问题 For an std::map<std::string, std::string> variables , I'd like to do this: BOOST_CHECK_EQUAL(variables["a"], "b"); The only problem is, in this context variables is const , so operator[] won't work :( Now, there are several workarounds to this; casting away the const , using variables.count("a") ? variables.find("a")->second : std::string() or even making a function wrapping that. None of these seem to me to be as nice as operator[] . What should I do? Is there a standard way of doing this

How to make_shared a derived class?

爷,独闯天下 提交于 2019-12-22 01:28:30
问题 I want to use the make_shared<T> function with a derived class, like below class Base { public: typedef std::shared_ptr<Base> Ptr; }; class Derived : public Base {}; Base::Ptr myPtr = std::make_shared(/* Derived() */ ); How can I tell make_shared to build such an object? I want to avoid the classical Base::Ptr ptr = Base::Ptr(new Derived()); To make use of the single alloc in the make_shared function. 回答1: std::shared_ptr has a converting constructor that can make a shared_ptr<Base> from a