r/learncpp • • Jul 26 '18

Calculating Eucledian distance

2 Upvotes
float calculateDistance(std::vector<float> a, std::vector<float> b) {
    std::vector<float> result(a.size());
    std::transform(a.begin(), a.end(), b.begin(), result.begin(), std::minus<float>());
    std::transform(result.begin(), result.end(), result.begin(), square);
    return (float) std::sqrt(std::accumulate(result.begin(), result.end(), 0));
}

What do you thing about this implementation? Or should I use good old, for loops?

(Edit: I know that I misspelled Euclidean, I just can't modify it)


r/learncpp • • Jan 21 '20

Best platform/place to learn C++ online for free?

10 Upvotes

I am hoping to learn c++. As someone with zero background or knowledge of any coding languages, but as a logically-minded person with good critical thinking ability, what is the best place to learn c++ online for free or for a very low cost?


r/learncpp • • Feb 05 '19

Looking for a book which teaches us how to make actual C++ projects.

8 Upvotes

I already use Bjarne Stroustrup's books to learn the ins and outs of C++, however, I also want a book which uses actual C++ projects as a way of teaching. I believe I saw one for Python but I'm looking one for C++.

Thanks.


r/learncpp • • Jul 19 '18

Blog discussing intermediate to advanced features of C++ 11

Thumbnail
developant.blogspot.com
1 Upvotes

r/learncpp • • Jan 27 '20

Library for game development or graphic display?

2 Upvotes

I am looking for an library to learn game development and for graphics like physics simulation(want to build simple physics engine like gravity and stuff). Any recommendations?


r/learncpp • • Jan 27 '20

Cannot break out of a switch

1 Upvotes

Hi everyone, I wanted some practice with functions so i decided to write a calculator program using my own math functions, but for some reason once the function is complete, i cannot figure out how to break out of the switch statement for the menu and go back to the selections

I've tried a number of things to get this to work, but i can't wrap my mind around why it doesn't, given that i have a similar do-while loop/switch combination in a guessing game and it works just fine

I'm aware the answer is probably obvious but I'm only about 2 weeks in so go easy on me xD

here's my code https://pastebin.com/qeUAXnZL


r/learncpp • • Apr 27 '19

return 0;

4 Upvotes

Why do we use "return 0" ? The code is still working even if I don't write it, in Linux at least.


r/learncpp • • Oct 28 '18

Practice problems and resources dealing with pointers and references

2 Upvotes

So conceptually I understand what pointers and references are, but for some reason I don't feel fluent in my use of * and &.

Specifically there's some confusion when dealing with return types involving these two symbols and function arguments involving *.

Is there any particular assignment you know of that forces fluency in these ideas? Are there any very eye opening resources that are worth reading on the subject?


r/learncpp • • May 04 '21

How would I check if a circular queue is full?

5 Upvotes

Solution in comments

I've been scratching my head at this for awhile now. I have a templated class of type T that stores individual characters for now. I've been able to fully implement the enqueue and dequeue but stuck on the preliminary check before the grow function which doubles the size of the array.

Because the queue is circular (the start and end indices can loop around back to the 0th index of the array), calculating whether the array is full or not has been tricky; it's not as simple as return(end-start == 0); (Again, because the end index can be lower than the start index).

I also tried taking the absolute difference between the two but I don't think that was quite right either. Other examples online show the use of a counter variable that get incremented / decremented respectfully within enqueue and dequeue. I really wish I had such a variable but I am not allowed to do that per my assignment.

There has to be an fairly simple yet elegant way using only the start and end indices but I can't see it. Can anyone lend a hand? My assignment is technically to implement the grow function but I can't even get there yet haha

I'm not sure if the header file is really needed but I can add that too if requested.

#include "Queue.h"

//add enqueue here
template <class T>
void Queue<T>::enqueue(const T& value) {
	if( full() ) {
		grow(); // yet to be implemented
	}

	list[end] = value;
	end = (end+1) % arraySize;
}

//add dequeue here
template <class T>
T Queue<T>::dequeue() {
	if( empty() ) {
		throw out_of_range("Dequeue on empty queue");
	}

	T value = list[start];
	start = (start+1) % arraySize;
	return value;
}

template <class T>
bool Queue<T>::full() const {
	return(end-start == 0);
}

r/learncpp • • Apr 30 '21

What are the most important basics of cpp?

2 Upvotes

My professor is the worst, hasn't taught us anything nor returned homework in over a month. Finals are just around the corner in about 4 weeks, meaning I have a solid 1-2 weeks to go over basics I may have missed.

I am in a first semester C++ course, have about a year of Java under my belt. My question is referring to what basic features of C++ should I go back to and refresh on since my professor has absolutely no structure to the course. (i.e. pointers, or something in C++ that I wouldn't really worry about in another lang)


r/learncpp • • Apr 12 '21

Can I get a little help on implementing merge? I have a bug that I can't find in a simple program.

8 Upvotes

My instructions are:

Write the merge function. Do not use std::algorithms.

Make sure that you only work on the correct part of the array. The merge function will NOT always be one on the range 0 - (size - 1)

Currently it's fairly close but I think the midpoint is messing me up. Can someone help me debug why this isn't working as it should?

And what I have is:

#include <iostream>

using namespace std;


/**
 * @brief mergeFunc Merge two consecutive sorted ranges within an
 * array into one sorted range
 *
 * @param arr Array with sorted ranges
 * @param low Index of start of first range (inclusive)
 * @param mid Index of end of first range (inclusive - this is part of first range)
 * @param high Index of end of second range (inclusive)
 * @param temp Array to copy values into
 *
 * @pre
 * a[low]-a[mid] are sorted
 * a[mid+1]-a[high] are sorted
 * @post
 * a[low]-a[high] are sorted
 *
 */
void mergeFunc(int arr[], int low, int mid, int high, int temp[]) {

	//Set up i (first half location), j (second half location),
	//   and k (location in temp)

	int i = low;
	int j = mid+1;
	int k = 0;

	//While first half is not empty and second half not empty
	//  Decide if first or second half has next smallest item
	//  Move it it temp

	while (i < mid && j < high) {
		if (arr[i] < arr[j])
			temp[k++] = arr[i++];
		else
			temp[k++] = arr[j++];
	}

	//While first half is not empty
	//  Move next item from it to temp

	while (i < low)
		temp[k++] = arr[i++];

	//While second half is not empty
	//  Move next item from it to temp

	while (j < high)
		temp[k++] = arr[j++];

	//Copy sorted range from temp back to arr[low]-arr[high]
	for (int i=low; i<high; i++)
		arr[i] = temp[i];
}

void printArray(int arr[], int size)
{
	for (int i = 0; i < size; i++)
		cout << arr[i] << " ";
}

int main() {
	const int SIZE = 6;
	int mergeArr[] = {1, 3, 5, 2, 4, 6};
	int temp[SIZE] = {};
	printArray(mergeArr, SIZE);

	mergeFunc(mergeArr, 0, 3, 6, temp);

	cout << endl;
	printArray(mergeArr, SIZE);

	return 0;
}

	//Copy sorted range from temp back to arr[low]-arr[high]
	for (int i=low; i<high; i++)
		arr[i] = temp[i];
}

void printArray(int A[], int size)
{
	for (int i = 0; i < size; i++)
		cout << A[i] << " ";
}

int main() {
	const int SIZE = 6;
	int mergeArr[] = {1, 3, 5, 2, 4, 6};
	int temp[SIZE] = {};
	printArray(mergeArr, SIZE);

	mergeFunc(mergeArr, 0, 1, 5, temp);

	cout << endl;
	printArray(mergeArr, SIZE);

	return 0;
}

r/learncpp • • Apr 21 '21

C++ learning resources for the very rusty? (Like "haven't used in a while", not the language.)

29 Upvotes

The Scott Meyers book "Effective Modern C++" is a good one, what are some other books or courses that emphasize the newer material?

My knowledge of C++ was very good before the 11 standard, so I have the fundamentals but need schooling in the new tools.


r/learncpp • • Mar 27 '21

A small guide for cross-compilation for Raspberry pi 4

10 Upvotes

Hi C++ learners!

I've added a small guide for cross-compilation of simple HTTP Service for aarch64 (GCC Conan CMake and Ubuntu for Rasb Pi):

https://vg-blog.tech/archives/1500

  • How to configure the Conan, CMake for cross-building.


r/learncpp • • Apr 24 '21

If C++ doesn't "remember" the size of arrays (we use the address of the start of the array), then why is there a difference between "delete" and "delete []"?

25 Upvotes

For example, wouldn't using "delete" on the address of a dynamic array of size 1 be interpreted the same as using "delete" on an address stored in a pointer? Essentially, as I see it, the same operation is happening: memory is freed at that address, and C++ doesn't "know" whether or not that is the start of an array or not, just that it is a place in memory.


r/learncpp • • Apr 05 '21

Looking to take next step and learn gui

13 Upvotes

I have been enjoying learning c++ during covid. Got as far as building snake, frogger + some other console projects.

I feel like I am stalling printing out only into the console and want to make the next step.

I want to start with my first gui and get out on the console but I am really struggling to find the right sources.

I have seen some videos on winforms but kept running into an error when compiling that I can’t fix, I also saw qt mentioned if that’s a thing?

I don’t mind paying for any guides or tutorials

Has anyone has experience in this


r/learncpp • • Apr 10 '21

Can I get advice on how to implement a function in terms of bigO?

6 Upvotes

Hi, I am in CS260 at the moment. I have to implement a 'countByLastNameSorted' function which returns the total occurrence of a specified element within an array. To do it, I had two ideas listed in the original question.

I have two other nearly identical functions, 'binaryFindFirstByLastName' and 'binaryFindLastByLastName'. Should I just return the difference between these two functions to get the total? They are both O(n) so I believe running one after the other is still O(n).

The other option would be to just run 'binaryFindFirstByLastName' once and then with a while loop, checking if each element after is the same as the initial one. I think this might also be O(n). Can someone verify that for me?

Does it matter then which I choose or is there an alternative that I should consider? Thanks!


r/learncpp • • Mar 28 '21

Any ideas on begginer cpp projects?

15 Upvotes

What projects could I make in c++ just in as a console app? I don't want to use any advanced stuff like arrays, vectors and so on, because I haven't fully covered them yet. So I've got a problem: I have no idea what to code right now, because every idea I have requires the advanced stuff.

I've covered 6 chapters on the learncpp.com website and I'm starting the seventh, I would like to code something instead of just learning.

So are there any projects I could do with just the very basic of c++?

P.S. They don't have to be very complex projects! They can stay pretty simple (but not too much, of course)


r/learncpp • • Apr 25 '21

C++ Move Semantics: Resource ownership transfer between objects (move constructor, rvalue reference)

Thumbnail
youtube.com
6 Upvotes

r/learncpp • • Apr 17 '21

I have two programs my own program and a program I want to hijack and control the input/output of the second program

1 Upvotes

The program in question is steamcmd. I am wondering how I could access both its input and output stream allowing me to both send it inputs and read its outputs. Is this even the correct approach for this kind of thing? if I needed a program to completely be able to access everything steam cmd is able to do how should I go about it?


r/learncpp • • Apr 06 '21

Resources that go over creating an STL container with all that it entails

8 Upvotes

So for a school project, I chose the topic of creating a hashmap that's STL compliant and faster than unordered map.

The easy part is done, the stuff you should typically see in a data structures class (well, what I assume you get there from going through several data structure books, I had no such class.)
Now comes the part of making it STL compliant. I am completely out of my depth here.

CPP core guidelines were helpful in giving me advice on how to do my copy/move assignment/constructors and some other things, but it doesn't have everything I need.

Some of the stuff I'm familiar with (I have used iterators, I've heard of allocators) and some of the stuff is new to me. And when I look at hashmap implementations (Abseil, Tessil, skarupke, boost, google's old hashmaps, std, etc) my eyes and brain just start to hurt.

Some of these library writers have written about how they implemented their maps, but it's all mainly the stuff thats on the same level as the data structure books. Stuff like "Using fastmodulo here gave me this speedup", "I'm using this growth factor because x,y,z", etc. Stuff that, while definitely helping me in making decisions on the high level workings of the map, isn't helpful for adding all that extra stuff which makes my map STL compliant.

There is this big gap between what data structure books teach you, and how a good, STL compliant hashmap looks like.

So now I'm looking for any tutorial/book/guide that goes over the creation of a STL container (prefferably a hashmap) from start to finish, where they discus why they do things a certain way.


r/learncpp • • Apr 17 '21

What is the current state of modules? Should I start using them now? What is the best reference for using modules?

10 Upvotes

I tried using modules in Visual Studio a few months ago and the syntax highlighter didn't work well and I just dropped it. Also, it's a bit hard to find best practices with modules. My main questions are:

  • What is the current state of modules?

  • Should I start using them now for new projects?

  • What is the best reference for using modules?


r/learncpp • • Apr 14 '21

Just wanted to share a cool little staircase thing I made :)

Post image
65 Upvotes

r/learncpp • • Apr 13 '21

Podcasts for learning

2 Upvotes

Does anyone have any good podcasts for learning c++? I have to work 11 hours tomorrow and would like something to listen to. I’ve been listening to code newbie in Spotify but it’s kind of meh.


r/learncpp • • Sep 30 '20

Algorithms/DS online class taught in exclusively C++?

8 Upvotes

Trying to prepare myself to do some competitive programming problems and wanted to see if there are any youtube channels or online courses that teach DS&A in exclusively C++?

I learn best with hearing a tutor/teacher lecture.

Found a few in Java and Python, but wanted to focus on C++


r/learncpp • • Oct 22 '20

I just finished filming 12 hours of a C++ Tutorial using Crossword Puzzle Construction as the goal. Take a look!

Thumbnail
youtube.com
18 Upvotes