std::vector memory allocation/free: always heap@@

https://stackoverflow.com/questions/8036474/when-vectors-are-allocated-do-they-use-memory-on-the-heap-or-the-stack is concise.

  • The vector “shell” can be on stack or heap or static memory, as you wish.
  • The continuous array + the payload objects are always allocated on the heap, so that the vector can grow or deallocate them.

Deallocation is explained in https://stackoverflow.com/questions/13944886/is-stdvector-memory-freed-upon-a-clear

max profit #max subarray sum is similar


/* max profit or biggest drop. one pass
max subarray sum -- is a similar problem, if you visualize the items as price changes
*/
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

template<typename T> void dump(vector<T> const & v){
	copy(v.begin(), v.end(), ostream_iterator<T>(cout, " "));
	cout<<endl;
}
int main(){
    vector<int> v={9,1,3,7,8,1,6,5}; //prices
	dump(v);

	// HighestProfit, LowestPx
	int hp=0, lpx=v[0];
	for (unsigned int i=1; i<v.size();++i){
		int aa=v[i];
		if (aa<lpx){
			lpx=aa;
			continue;
		}
		int profit = aa-lpx;
		if (profit > static_cast<int>(hp)){
			cout<<profit<<" > "<<hp<<endl;
			hp=profit;
		}
	}
	cout<<"ret = "<<hp;
}

combine cheatsheets on cStr+std::string@@

If a coding test requires string operations A1 A2 A3 A4… and B1 B2 B3…, and I know how to do A1 .. on cStr and B1… on std::string, is it practical to rely on 2-way conversion? Will we be able to get the job done?

If possible, then could we save the effort or learning two solutions on every string operation.

It’s more elegant to use only std::string exclusively, but I don’t have the bandwidth…

range-check in c++ vector^raw array

[[safe c++]] points out that static array or dynamic array are both (unfortunately) silent about access beyond their limits. Vector has operator[] and at() —

[[]] says c++ new array type supports .size()…


#include <iostream>
#include <vector>
using namespace std;

int main(){
    vector<int> v;
    v.push_back(15);
    size_t sz = v.size();
    try{
        cout<<v[0]<<" "<<v[sz]<<" <- operator[]: out-of-range treated as non-error!"<<endl;
        cout<<v.at(sz)<<" <- at(): throws:)"<<endl;
    }catch(exception & ex){
        cerr<<"Must catch by ref (what() virtual) " << ex.what()<<endl;
    }
}

cStr checkHasText()

bool checkHasText(char const* c){
    unsigned int const len = strlen(c);
    unsigned int i = 0;
    for (; i <= len – 1; ++i) {
        if (!isspace(c[i]))
            break;
    }
    if (i == len) {
        cout << "all space\n";
        return false;
    } else {
        cout << "has text\n";
        return true;
    }
}

Alternatively, here’s a pointer-arithmetic version, more direct, less readable

bool checkHasText(char const* c){
    for (; *c; ++c)
        if (!isspace(*c))
            break; //better than return true — can check *c
    return *c; //implicitly cast to bool.
//    if (*c) {
//        cout << "has text\n";
//    } else {
//        cout << "all space\n";
//    }
}