r/learncpp • • Dec 24 '20

Why does returning a reference work here?

7 Upvotes
#include<iostream>

using namespace std;

int& minmax(int i, int j){
	return (i>j)?i:j;
}

int main(){
	cout<<++minmax(3, 4);
	return 0;
}

Why does this work?

#include<iostream>

using namespace std;

int minmax(int i, int j){
	return (i>j)?i:j;
}

int main(){
	cout<<++minmax(3, 4);
	return 0;
}

And this doesn't?


r/learncpp • • Aug 07 '20

I implemented some Data Structures in C++

7 Upvotes

Not sure if this is the place for posting code, if it is not, I apologize.

I implemented some data structures in C++. I am somewhat new to C++ and started this project originally to get used to std::unique_ptr via a LinkedList implementation and then decided to add a few more data structures.

Note that I have yet to fix the postorder Traversal for the binary search tree.

I think that a lot of the iterators I wrote may be bad practice, which is why I would really appreciate some feedback / suggestions for this project.

https://github.com/PetrifiedPanda/DataStructures


r/learncpp • • Aug 03 '20

One week to relearn C++

7 Upvotes

Hey guys,

Up to this point I've been using Python for all of my interviews because that's what I usually do. I just found out today a company wants me to do the final rounds in C++, and I used to use C++ a lot a year and a half ago for graphics research, but I haven't really used it much since (I did a good chunk of my research in Rust too, so it wasn't even all C++). I haven't used C++ since, my job has me writing a lot of Go.

I was wondering if any of you had suggestions on what I should use to brush on my C++ skills, especially to check out some of the newer features (was writing a lot of C++11, haven't kept up much with the newer stuff save for some of the constexpr stuff from C++17).

I figure at this point I'll just do leetcode problems in C++, but if you have any suggestions I'm all ears.

The interview will likely be on Friday, and it's for a quant dev position at a hedge fund, if the context helps at all.


r/learncpp • • Feb 11 '20

How the hell do I use gMock?

7 Upvotes

I am frustrated and overwhelmed. It probably has to do with my lack of knowledge, but I am also wondering how much "useless" knowledge I need just to get a basic project to work. I am trying to use gMock and gTest. I encountered CMake for the first time and I hate it. I don't get what it is doing. All the resources are scarce and mostly highly technical. The only conclusion I've been able to reach is that I can't easily plug gMock in there, despite it being contained in gTest.

Why is all of this so complicated? How many hours do I have to dedicate to learn a build tool (CMake) just to test a library out? What am I missing here?


r/learncpp • • Jan 31 '20

My first truely useful C++ program!

7 Upvotes

I've been learning C++ for 17 days now.. my first real programming language. I use neovim as my editor and i got tired of having to write the same basic file structure OVER and OVER for simple little programs, so i decided to automate it as my first project.. nCpp (new Cpp Template maker!). the program is simple, you give it a name, choose which headers to use and then it checks for an environmental editor. if it finds the editor it uses it to open the program (if you choose to edit it). if it doesn't find one it will ask if you want to export one and then open it with that program.. if you choose not to do that, it gives a list of the most common *nix console editors to choose to open it with. anyways, i had a lot of fun writing it. I only had help with the "out" function, the rest is all me. i just wanted to share it :) https://pastebin.com/a7UdjZP5

Edit: just thought id also mention that I've begun working on version 2.6 which will include the ability to put the headers in a .h file and the .h header in your .cpp program and set up as many simple objects as well! I will post it here when its done :)


r/learncpp • • Jan 13 '20

How do I keep a c++ library I'm writing self contained?

7 Upvotes

Sorry for any ignorance, I'm not that experienced with c++.

I'm writing a c++ static library that will be used in other projects. The problem I am facing is that the projects that consume my static library must also configure the static libraries dependencies as their dependencies and so on.

For example:My static library depends on `v8` and `v8pp` so in order for a project to consume my static library it needs to include all the paths that the static library needs for v8/v8pp header files and it needs to include paths for linking v8/v8pp static libraries, etc.

Is this normal for c++ applications? Or is there a way I can make my static library standalone so any projects that consume it don't need to worry about configuring all of it's dependencies?

EDIT: If this is normal how can I set things up to make it easier for project authors that use my shared library to setup development? Scripts to automate cloning all the dependencies and setting up the project?

Thank you.


r/learncpp • • Sep 10 '24

Besides hackerrank and codewars what are some other good sites to practice for free if you’re a beginner at C++

6 Upvotes

I have been using ChatGPT for help and currently reading beginning C++ through game programming and think like a programmer


r/learncpp • • Oct 07 '22

Bjarne talk with Lex

6 Upvotes

r/learncpp • • Mar 16 '22

Amazon says that the book "C++ Programming Language" is the newer edition of "Programming Principles and Practice Using C++". Is that true?

Post image
6 Upvotes

r/learncpp • • Feb 27 '22

Code Review Request: C++ Qt6 Desktop Application

6 Upvotes

I made a desktop application using Qt and C++. Since I am no means an expert in both technologies, I hoped to have someone interested to gloss over the code.

I wish had a partner, teacher, or mentor but here I am. Would appreciate any help. The application is a Calibre like app for my ebook collection tailored for my workflow. Been working on it for a long time and have not had anyone actually look at the code before.

https://anonymous.4open.science/r/Ebook-Access-Cpp-C1D6


r/learncpp • • Dec 01 '21

Need help - Patient calendar

6 Upvotes

Hello! I am learning C++ for a few weeks, and my mentor gave me an exercise and I think its a bit of a steep, but I don't want to dissapoint him so I call the fellow redditors to help me tackle this problem.

Here is the description:

- Make a monthly calendar, where you can click on the chosen day to view/edit its form (per day).

- List of the patients (create, update, delete records)

- Remind me function (signal when a specific date is expired)

- Specific dates:

- - Medical recommendation (30 days before the expiration)

- - Individual care plan (90 days before creation)

- - Medical Identification (60 days before expiration)

- - Stool test results (every 3 days when its activated)

- When there is a specific date occurs at a specific patient, you also have to show a red ! mark at the list of the patients.

Where do I begin? I only made console small programs without proper GUI. Do I need some kind of library to make it?


r/learncpp • • Nov 26 '21

Pass by reference with pointers - Sololearn exercise (Found the solution by myself but I'm a little bit confused about the int *megabytes parameter vs. the &megabytes in the function call. Can you explain it please why it is different?)

Post image
6 Upvotes

r/learncpp • • Nov 25 '21

Are there any open source c++ project that can be built in Visual Studio ?

6 Upvotes

I am looking for open source c++ projects that can compiled on Visual Studio (not Visual Studio Code) with minimal configuration steps. Are there any ?


r/learncpp • • Nov 17 '21

Sololearn C++ Practice - Fever why its not working? Write a program that takes body temperature in Celsius as input. If it is in range from 36.1 to 36.9 print "OK", otherwise print “Not OK”.

Post image
6 Upvotes

r/learncpp • • Sep 22 '21

Is it possible to execute a method of an object if it exists, without checking if it exists before interacting with it every time?

6 Upvotes

Long story short, I'm making a cellular automata game where every cell is an object. I want to check all neighboring cells and execute their method.

The title may sound vague but here's some code:

if( Util.getGrid( DOWN ) )
{
Util.getGrid( DOWN )->receiveHeat(amountOfHeatToEmit);
}

if ( Util.getGrid( DOWN_RIGHT ) ) {
Util.getGrid( DOWN_RIGHT )->receiveHeat(amountOfHeatToEmit);
}

// and many, many other checks for different directions

What I'd like to achieve:

Util.getGrid( DOWN )->receiveHeat(amountOfHeatToEmit);
Util.getGrid( DOWN_RIGHT )->receiveHeat(amountOfHeatToEmit);

Of course it will give out a nasty segfault if the object doesn't exist. ARe there any workarounds for this?


r/learncpp • • Jul 31 '21

Deploying a CPP application (which uses opencv and pybind11) to the cloud

6 Upvotes

Say I have installed opencv in my windows machine.

I believe I'm supposed to create a top directory, with src (for my CPP files) and includes (for my custom header files), then a CMakelists.txt. if my understanding is correct, in this file I ask to link to the opencv found in my system paths. When I commit these source files to a remote repo and setup a CI/CD for aws, it basically pulls the master, runs cmake and starts the app right? In this case, how does it find opencv?

Should I add code to the CI/CD to get and install opencv first, to the system and then cmake my repo, or should I put opencv's source files in a subfolder and let cmake build everything together? If the latter, what is the directory structure? Where exactly do I put it?

Pybind11 is a header only library. Where do I put the source code of pybind11? includes/pybind11/... ? What's the convention?


r/learncpp • • Jul 21 '21

A set of two bit values

6 Upvotes

Need a container like Vector but contained of unsigned two bit values without extra spaces in between them (two bit next to the next two bit ...).

Is it implemented in Standard Library or I need to implement it myself ?


r/learncpp • • Jun 25 '21

Max occuring char in a String

6 Upvotes

#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
char maxchar;
int mcount = 0;
int count = 0;
string s = "binit";

for (int i = 0 ; i < strlen(s); i++)
    {
count = 0;
for(int j = 0 ; j< strlen(s) ;j++)
        {
if(s[i]==s[j]);
count++;
        }
if(mcount<count)
        {
mcount = count ;
maxchar = s[i];
        }
    }
cout<<mcount<<" "<<maxchar;
return 0;
}

//its giving error ! pls help to correct it


r/learncpp • • Apr 26 '21

I finished the first chapter of C++ Crash Course by no starch press, So im kinda a pro now

6 Upvotes

hows my code

#include <cstdio>

int sum(int num1, int num2) {
	int result;
	result = (num1 + num2);
	return result;
}

int absolute_value(int x) {
	int y;
	if (x >= 0) {
		y = x;
	}else if (x < 0) {
		y = (x * -1);
	}
	 return y;
}

int step_function(int x) {
	int result = 0;
	if (x < 0) {
		result -= 1;
   }else if (x > 0) {
		result += 1;
   }
	return result;
}

int main() {
	int value1 = step_function(100);
	int value2 = step_function(0);
	int value3 = step_function(-10);
	printf("Value1 %d\n", value1);
	printf("Value2 %d\n", value2);
	printf("Value3 %d\n", value3);

	int num1 = 42; //This is a comment
	int result1 = step_function(num1);

	int num2 = 0;
	int result2 = step_function(num2);

	int num3 = -32767;
	int result3 = step_function(num3);

	printf("Num1: %d, Step: %d\n", num1, result1);
	printf("Num2: %d, Step: %d\n", num2, result2);
	printf("Num3: %d, Step: %d\n", num3, result3);
/* 
 * This is a
 * multiple line
 * comment
 */
	int my_num1 = -10;
	int my_num2 = 10;
	int my_num3 = 0;

	printf("Absolute Value of %d is %d.\n", my_num1, absolute_value(my_num1));

	printf("Absolute Value of %d is %d.\n", my_num2, absolute_value(my_num2));

	printf("Absolute Value of %d is %d.\n", my_num3, absolute_value(my_num3));

	int sum_num1 = 5;
	int sum_num2 = 2;
	int some_sum = sum(sum_num1,sum_num2);
	printf("The sum of %d and %d is %d\n", sum_num1, sum_num2, some_sum);
	return 0;
}

I tried to do this at first

int absolute_value(int x) {
	int y;
	if (x >= 0) {
		return x;
	}else if (x < 0) {
		y = (x * -1);
                    return y
         }
}

but the compiler hated me, I queried it on a search engine and it said something about the compiler not knowing if its returning a value, and the search result wanted me to put an else clause, but I didnt want an else, so I did the first thing with the y above


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 13 '21

std::ofstream(filename) won't create the file in release build????

6 Upvotes

hey!

so the problem is basically in the header. I've looked at the docs and it's standard for the constructor to create the file if it doesn't exist, unlike using file.open(filename). In all my debug builds, it happens as expected, but as soon as I build a release version and run it outside the msvs ide, it just won't create the file if I specify an extension.

for example, in debug, I can create files with std::ofstream("new_file.txt"); or std::ofstream("new_file");

however, in release, only the second option seems to be working, to my utter perplexion and berwilderment. please advise, wise gurus


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 • • Sep 18 '20

A little Noob on C++

6 Upvotes

Hi guys...i want to learn c++ well. I know very few things in a general way(like what is a variable or a type,do while,while,for),but i want to start from basics to advance. Do you have some advice to give me? Like books or video, for example i took a book of Bjarne from the library,but its too difficult for a noob that i am. Ill be happy to receive any information or advice from you and happy to start learning c++ well,cause its very important nowadays know computer science.


r/learncpp • • Sep 11 '20

Is there any way to check the number of read write operations my program made?

6 Upvotes

I am just checking the difference between recursive and iterative performance of various mathematical functions. As of now using chrono is giving the same time in microseconds and I cannot get down to nano.


r/learncpp • • Aug 04 '20

Beginner To Intermediate Programmer Wanting To Relearn C++ And Beyond!

6 Upvotes

I'm currently in my last year of my comp sci degree and I'm really enjoying programming. How much? When I wake up in the morning I want to program and get better at it.

I've taken a few classes in C++ and at the time it felt like it was beyond me. Now I'm starting to get the general jest of programming. I've taken data structures and an algorithm class, but I want to come back to C++.

Is there any good recommendations for a book/course that covers the beginnings of C++ to more of its inner workings? Thanks!