« first day (914 days earlier)      last day (1939 days later) » 

9:08 AM
Hey guys, any idea why i get the following error message with this code? https://pastebin.com/n5wsgqeV
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 4294967295) >= this->size() (which is 52)
 
nwp
Your compiler should have warned you that i >=0 is always true.
 
0 errors, 0 warnings
 
nwp
Change warning flags or get a better compiler.
Anyway, do you see why it's always true?
 
ohhh i get it, because size_t can't be negative. but still i think this should work
as i want to access the index 0 in the last iteration... how should i proceed then... let me think
so is the best solution to replace size_t with int?
it works with int, but maybe there is a better solution
 
nwp
Maybe. Using iterators and rbegin/rend is an alternative.
 
9:16 AM
ok, thx!
which IDE would you guys suggest?
 
nwp
Try out VS, VS Code, Qt Creator, CLion, Eclipse, Vim, Emacs, Codeblocks, ... and see which one you like most. It's personal preference mostly.
 
 
3 hours later…
12:06 PM
Hey I asked this before but kinda didn't get a satisfying solution. How can I plot stuff easily in c++?
without having to use a special compiler
 
 
4 hours later…
SJD
4:00 PM
hello. I have a question regarding how should I perform an operation better. I have 128 x std::vectors<float> - each having 2.5 million values. The operation in which I populate them takes about 600ms. However, I would like to store their references in another std::vector (should say, I will have a std::vector<std::vector<float>>). After putting the vectors in their position, the time spent on this operation is 800ms.
I wonder what takes so much to copy the reference of all these 128 vectors in a new vector... Anyone can help me with this? (I believe though that I do something wrong)
 
2 messages moved from Lounge<C++>
 
nwp
@SJD Hard to tell without seeing the code. Look for unnecessary copies of the vectors.
 
SJD
std::vector<int> getVec()
{
    return std::vector<int>(2500000);
}

int main() {

    std::vector<std::vector<int>> myM(10);

    auto start = std::chrono::high_resolution_clock::now();

    for (int i =0; i < 10; i++)
        myM[i] = std::move(getVec());

    auto finish = std::chrono::high_resolution_clock::now();
    std::cout << "Time: " << std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count() <<  "micro | "
                << std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count() << "ms " << std::endl;
I illustrated quite simple, what I would like to achieve
If I remove this assignment: myM[i] = .... (the time will substantially decrease)
this takes about 144ms, but if I remove the assignment, it will take 70ms only
 
nwp
4:16 PM
myM[i] = std::move(getVec()); should be myM[i] = getVec();.
 
SJD
Yes, but seem to be still around 140ms
I guess it does more than just assigning the vectors addresses to the location in myM
 
nwp
Have you enabled optimizations?
The assignment sets 6 pointers. It takes less than 1ms.
 
SJD
Activated -o3 (using clang) now seem to be around ~100ms
 
nwp
I would expect clang to optimize out getVec when you don't assign it to anything.
 
SJD
without the assignment, it is 119 micro (0ms)
 
nwp
4:22 PM
Seems reasonable.
 
SJD
still wondering if I can optimize the way I set the references in myM.
RVO should do the thing, but read in multiple places that std::move should be used if I don't need the value afterwards (that's why I added it, hoping for better performance)
but if this is the best I can get, I'm good with it. I just wanted to make sure that I'm not doing something awkward :D
 
nwp
std::move makes it treat a variable as a temporary. getVec() already returns a temporary, so it does nothing at best and hinders optimization at worse.
In other words you couldn't use what getVec() returns afterwards even if your wanted to.
 
 
5 hours later…
9:03 PM
The most feature I love in C/C++ is function pointers as follows.

void PrintList(bool(*f)(int), int data[], int N)
{
	for (int i = 0; i < N; i++)
		if (f(data[i]))
			cout << data[i] << endl;
}

bool isOdd(int x)
{
	return x % 2 == 1;
}

int main()
{
	int data[] = { 1,2,3,4,5,6,7,8,9 };
	PrintList(isOdd, data, 9);
	cin.get();
}
What is your favourite features?
 

« first day (914 days earlier)      last day (1939 days later) »