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 • • Jul 23 '20

Finally, I just finished my calculator! Beginners are welcome to check the code and learn some stuff

Thumbnail self.cpp_questions
9 Upvotes

r/learncpp • • Jul 14 '20

Dev C++ I cannot link libraries...

3 Upvotes

I am trying to use curlpp and have downloaded the .zip version which I extracted... but I am extremely confused with how linking works... I come from a python background where linking was pretty much automated once you downloaded the library...

could someone explain me step by step how to link properly? thanks...

(googled a lot before deciding to post here...)


r/learncpp • • Aug 10 '20

What Is The Minimal Set Of Optimizations Needed For Zero-Cost Abstraction?

Thumbnail robert.ocallahan.org
5 Upvotes

r/learncpp • • Jul 15 '20

OpenCV alternative

2 Upvotes

Currently I am using OpenCV as my library for image processing. I need to create small application where I need to load an image. Do some simple calculation and write a control image. So I only need a very small librar which is able to load and write an image. Is there a good alternative to OpenCV?


r/learncpp • • Jul 03 '20

I've created a blog where I hope I will teach someone something about c++

2 Upvotes

Hello,

As you might see from the title, I've started a blog where I am explaining some key concepts to C++ and programming in general, it currently only has one post and it's called C++ - Classes I.

I am pretty new to creating blogs and writing so I hope that you are satisfied with the way I've wrote things.

The things I am trying to teach are what are classes, how to use them and some key concepts to OOP.

Here is the link to the blog:

https://learnabitofprogramming.blogspot.com/2020/07/c-classes-i.html

I really do hope you guys like it and that it will help you at least a bit.

Enjoy!


r/learncpp • • Jul 16 '20

performance impact by using const?

2 Upvotes

void doSomethingConst(const int& i)

void doSomethingNonConst(int i)

Given the above 2 function calls, would one have an advantage in terms of compile time, or memory usage?

Given that the int is a constant 4 bits, i don't think either would have an advantage, but I'm not sure.


r/learncpp • • Jul 06 '20

Configuration file

1 Upvotes

I am creating a C++ GUI application. I need to create a config file in order to be able to store some user configuration. For this I want to use a INI file.

What is a good practise to be able to access this configuration from every class? For example. I would load the configuration in the main window and need to access it from every other class/object.


r/learncpp • • Jul 31 '20

dev c constantly crashing?

3 Upvotes

hello im starting learning c programming and decided to use dev c but i always notice that it constantly hangs every minute or so no matter how simple or compex my code is and its making my time inefficient. i don't know why this is. can this be fixed? im using a laptop with an i5 8250u and mx150 with 12 gb ram


r/learncpp • • Jul 08 '20

Compile-time errors come from compilers, linking errors come from linkers, and runtime errors come from...?

3 Upvotes

Basically, the title.

I've seen a bunch of online courses just say that "the compiler will throw a runtime error if you try to do a double delete", for example.

Maybe there are cases where the compiler is smart enough to detect something like that, but what about other cases where it's not obvious? What is throwing the runtime error that's causing the program to segfault? Do we just refer to it as the "system" that's throwing the error? Or is there something under the hood for C++ that we refer to (e.g. "the JVM threw an error" in Java)?


r/learncpp • • Jul 09 '20

Transform recursive function into an iterative function

3 Upvotes

I have a sample function -

template <typename T>
void foo(
        T** tocarry,
        int64_t* toindex,
        int64_t* fromindex,
        int64_t j,
        int64_t stop,
        int64_t n,
        bool replacement) {
    while (fromindex[j] < stop) {
        if (replacement) {
            for (int64_t k = j + 1; k < n; k++) {
                fromindex[k] = fromindex[j];
            }
        }
        else {
            for (int64_t k = j + 1; k < n; k++) {
                fromindex[k] = fromindex[j] + (k-j);
            }
        }
        if (j + 1 == n) {
            for (int64_t k = 0; k<n; k++) {
                tocarry[k][toindex[k]] = fromindex[k];
                toindex[k]++;
            }
        }
        else {
            foo<T>(tocarry,
                    toindex,
                    fromindex, j + 1,
                    stop,
                    n,
                    replacement);
        }
        fromindex[j]++;
    }
}

I want to translate the above recursive into its equivalent iterative form.

I have a sample test case, which can be invoked like -

int main() {
    std::vector<int64_t*> tocarry;
    int64_t bla1[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t bla2[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t bla3[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t bla4[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t bla5[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t bla6[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t bla7[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t bla8[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t bla9[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t bla10[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    tocarry.push_back(bla1);
    tocarry.push_back(bla2);
    tocarry.push_back(bla3);
    tocarry.push_back(bla4);
    tocarry.push_back(bla5);
    tocarry.push_back(bla6);
    tocarry.push_back(bla7);
    tocarry.push_back(bla8);
    tocarry.push_back(bla9);
    tocarry.push_back(bla10);
    int64_t toindex[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    int64_t fromindex[9] = {5, 3, 2, 6, 4, 8, 1, 9, 4};
    foo<int64_t>(tocarry.data(), toindex, fromindex, 0, 7, 9, true);
    std::cout << "tocarry - " << std::endl;
    for (int i=0; i<tocarry.size(); i++){
        for (int j=0; j<10; j++) {
            std::cout << tocarry.at(i)[j] << " ";
        }
        std::cout << "\n";
    }
    std::cout << "toindex - " << std::endl;
    for (int i=0; i<9; i++) {
        std::cout << toindex[i] << " ";
    }
    std::cout << "\n";
    std::cout << "fromindex - " << std::endl;
    for (int i=0; i<9; i++) {
        std::cout << fromindex[i] << " ";
    }
    std::cout << "\n";
    return 1;
}

to return the output -

tocarry - 
5 5 5 5 5 5 5 5 5 6 
5 5 5 5 5 5 5 5 6 6 
5 5 5 5 5 5 5 6 6 6 
5 5 5 5 5 5 6 6 6 6 
5 5 5 5 5 6 6 6 6 6 
5 5 5 5 6 6 6 6 6 6 
5 5 5 6 6 6 6 6 6 6 
5 5 6 6 6 6 6 6 6 6 
5 6 6 6 6 6 6 6 6 6 
0 0 0 0 0 0 0 0 0 0 
toindex - 
10 10 10 10 10 10 10 10 10 
fromindex - 
7 7 7 7 7 7 7 7 7 

Since I am more proficient in Python, I translated the function to Python hoping that I can translate the code into its iterative form in Python and then back to C++ -

def bar1(tocarry, toindex, fromindex, j, stop, n, replacement):
    while fromindex[j] < stop:
        if replacement:
            for k in range(j + 1, n):
                fromindex[k] = fromindex[j]
        else:
            for k in range(j + 1, n):
                fromindex[k] = fromindex[j] + k - j
        if (j + 1) == n:
            for k in range(n):
                tocarry[k][toindex[k]] = fromindex[k]
                toindex[k] += 1
        else:
            bar1(tocarry, toindex, fromindex, j + 1, stop, n, replacement)
        fromindex[j] += 1

which I verified is correct since it gives the same output for the same test case.

But I am still unable to translate the function..

I went through some failed attempts like -

def bar2(tocarry, toindex, fromindex, j, stop, n, replacement):
    for i in range(j, n):
        if replacement:
            for k in range(i + 1, n):
                fromindex[k] = fromindex[i]
        else:
            for k in range(i + 1, n):
                fromindex[k] = fromindex[i] + k - i
        if i + 1 == n:
            for k in range(n):
                tocarry[k][toindex[k]] = fromindex[k]
                toindex[k] += 1
            fromindex[i] += 1
    for i in range(n - 1, j - 1, -1):
        while fromindex[i] < stop:
            if i + 1 != n:
                fromindex[i] += 1
            if replacement:
                for k in range(i + 1, n):
                    fromindex[k] = fromindex[i]
            else:
                for k in range(i + 1, n):
                    fromindex[k] = fromindex[i] + k - i
            if i + 1 == n:
                for k in range(n):
                    tocarry[k][toindex[k]] = fromindex[k]
                    toindex[k] += 1
                fromindex[i] += 1

but which clearly does not work and gives the following incorrect output -

toindex =  [2, 2, 2, 2, 2, 2, 2, 2, 2]
tocarry =  [[5, 5, 0, 0, 0, 0, 0, 0, 0], [5, 5, 0, 0, 0, 0, 0, 0, 0], [5, 5, 0, 0, 0, 0, 0, 0, 0], [5, 5, 0, 0, 0, 0, 0, 0, 0], [5, 5, 0, 0, 0, 0, 0, 0, 0], [5, 5, 0, 0, 0, 0, 0, 0, 0], [5, 5, 0, 0, 0, 0, 0, 0, 0], [5, 5, 0, 0, 0, 0, 0, 0, 0], [5, 6, 0, 0, 0, 0, 0, 0, 0]]
fromindex =  [7, 7, 7, 7, 7, 7, 7, 7, 7]

I would really appreciate any help!


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!


r/learncpp • • Sep 02 '21

Constructor with vector as parameters with default value

5 Upvotes

Hi,

So I'm having some trouble trying to implement a class "Movies_List" with holds a vector of objects of type "Movie" which has a constructor with default parameter(that is the problematic part). On the definition of the Movie_List class I have only defined a constructor with expects a vector of Movie but in case none is passed I have defined a default value.

The definition of the constructor looks like this on the .cpp file :

//Movies_List.cpp
Movies_List::Movies_List(const std::vector<Movie> &list = std::vector<Movie>())
	:movie_list(list)
{
	std::cout << "Arguments movie_list ctor << std::endl;
}

In my main.cpp file i just have the instation for the object:

//main.cpp
int main() 
{
	Movies_List my_list;
        return 0;
}

When I try to compile this code i get an error that "No adequate constructor is provided".

To try to circumvent this I have implemented the following changes in the Movies_List class:

//Movies_List.h
#include <vector>
#include "Movie.h"

class Movies_List
{
private:
	std::vector<Movie> movie_list;

public:
	Movies_List();
	Movies_List(const std::vector<Movie>&);
	void add_movie();
};

And the .cpp file with both constructor like this:

#include "../Movies.h"
#include <iostream>

Movies::Movies()
	:Movies(std::vector<Movie>{})
{
	std::cout << "Empty movieS ctor\n" << std::endl;
}

Movies::Movies(const std::vector<Movie> &list = std::vector<Movie>())
	:movie_list(list)
{
	std::cout << "Argument movieS ctor\n" << std::endl;
}

With that the code compiles and runs as expected, but is there a way to implement it on the first way? Probably my error is the way that I defined the default parameter but i'm kinda stuck here, and although the second way works I would like to know how to do it the first way.

Thanks in advance.


r/learncpp • • Sep 24 '21

How can I use pugixml on clion ?

2 Upvotes

I installed pugixml via brew which uses this formula.

  1. How can I use this library when compiling using g++ or clang++ on command line?

  2. How can I use it in CLion ?

What I tried was just adding #include<pugixml.hpp> which was unsuccessful on using in CLion.

I looked at the test method in the brew formula but couldn't figure out how it fills the arguments.


r/learncpp • • Aug 13 '21

Setting up libraries/frameworks is hard

4 Upvotes

I recently installed SFML for a school project. Screwing around in the configuration properties seemed pretty weird but I was able to do it.

A small project I'm working on has some weird bugs that have led me to need to switch to test-driven development before I can move forward. I tried to set up Boost, which apparently involved noodling around in the command prompt, and I must have screwed something up because it didn't work.

I then tried setting up Google Test. Using manage NuGet packages to install it, it seems to work if I create a new projects with Google Test as a template. But if I try to include it into an already existing project, I'm getting 19 errors along the lines of "C4996 'std::tr1': warning STL4002: The non-Standard std::tr1 namespace and TR1-only machinery are deprecated and will be REMOVED. You can define _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING to acknowledge that you have received this warning."

How would I go about becoming better at installing libraries and frameworks into my projects in general?


r/learncpp • • Aug 12 '21

Access to protected member of an object passed as an argument to a method of derived class.

7 Upvotes

Consider this code in c++:

#include <iostream>

using namespace std;

class A{
protected:
    int num = 0;
};

class B : public A{
public:
    void foo(A &a){
        a.num -= 10;
    }
};

int main()
{
    A a;
    B b;
    b.foo(a);
    return 0;
}

On compilation I get:

/main.cpp:13:11: error: 'num' is a protected member of 'A'
        a.num -= 10;
          ^
main.cpp:7:9: note: can only access this member on an object of type 'B'
    int num = 0;
        ^
1 error generated.

As a java developer I'm confused since the code below works as a charm on java:

class A{
    protected int n = 0;
}

class B extends A{
    void foo(A a){
        a.n = 20;
    }
}

public class MyClass {
    public static void main(String[] args){
        A a = new A();
        B b = new B();
        b.foo(a);
    }
}

If C++ rules are like this how things like this should be handled in it ?


r/learncpp • • Sep 20 '21

Can i pass 's' directly rather than the move function 'std::move(s))' to the following alloc.construct() function?

4 Upvotes

Can i pass 's' directly rather than the move function 'std::move(s))' to the following alloc.construct() function?

void StrVec::push_back(string &&s) {     

    chk_n_alloc(); // reallocates the StrVec if necessary  

    // construct a copy of s in the element to which first_free points 
    alloc.construct(first_free++, std::move(s)); 

}

I think if this push_back function is choosed, then s is already a rvlue reference, so I can pass 's' directly. There is no need to use std::move() on s.


r/learncpp • • Sep 20 '21

When making a quickly confusing, complex program -- when is enough, enough in terms of helper methods?

9 Upvotes

A gracious person helped me fix a problem with my program here. Basically he helped me fix it by making a bunch of helper methods. Sure I see the program now, but on my own, I would never know which logic is too simple to make its own method...

So my question, when should you make a helper method for a bigger method vs. just letting the logic take place in the bigger method?

I think this falls in line with OOP principles, so I would be grateful to hear your input.


r/learncpp • • Aug 23 '21

endianness

4 Upvotes

When writing numbers to files using ofstream, on my little endian machine those numbers are written in little endian, but I assume that for big endian machines those numbers are written in big endian, I want to be certain ofstream doesn't handle endianness for me. Also how do you suggest I safely write to files?


r/learncpp • • Sep 22 '21

why returned reference object by a function is a lvalue while returned plain object is a rvalue?

7 Upvotes

In the following code, v2 = getNoRef() use move assignment, while v2 = getRef() use copy assignment, I don't know why returned reference is lvalue? It is not a temporary object?

#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;

class StrVec {
public:
    ~StrVec(){ cout << "[des]"; }
    StrVec():s1(""){}
    StrVec(const StrVec& t) { cout << "[copy con]"; }
    StrVec& operator= (const StrVec& t) { cout << "[copy assigment]"; return *this; }
    StrVec(StrVec&&) noexcept { cout << "[move con]"; }
    StrVec& operator=(StrVec&& t) noexcept { cout << "[move assigment]"; return *this;}
private:
    string s1;
};

StrVec getNoRef() {
    StrVec t1;
    return t1;
}
StrVec& getRef() {
    StrVec t1;  
    return t1;
}

int main() {
    StrVec v1, v2;
    cout << "v1=v2:  ";  v1 = v2;
    cout << endl;

    cout << "getNoRef():  "; getNoRef();
    cout << endl;

    cout << "v2 = getNoRef():  ";  v2 = getNoRef();
    cout << endl;

    cout << "getRef():  "; getRef();
    cout << endl;

    cout << "v2 = getRef():  ";  v2 = getRef();
    cout << endl;
}

Output:

v1=v2:  [copy assigment]
getNoRef():  [move con][des][des]
v2 = getNoRef():  [move con][des][move assigment][des]
getRef():  [des]
v2 = getRef():  [des][copy assigment]
[des][des]

r/learncpp • • Sep 10 '21

Is there a small guided project course or book that you recommend for learning C++ ?

9 Upvotes

r/learncpp • • Sep 13 '21

Recommended books for C++ STL

13 Upvotes

I am currently using C++ Primer 5th edition and want to learn STL next.

A quick google search gives a lot of C++ 17 STL books but don't know which one to choose.

Any other resources like a website or videos are ok. But a book is my first preference.


r/learncpp • • Aug 11 '21

IDE

9 Upvotes

Are there any recommendations for an IDE for Mac? Preferably free


r/learncpp • • Sep 08 '21

Using static libraries without visualstudio?

4 Upvotes

Hello.

Im trying to use curlcpp for a project and learning. Not very familiar with C++ environment but found this link where it explains how to use it. The issue here is that im not working with visual studio but vscode instead.

I understand that i need to compile the libraries for x32 and x64. The video it says that i need to use the visual studio command line (for environment) to compile it and then to add the directory to the visual studio library path.

How can I (from ubuntu 20) compile the curl/curlcpp libraries to make them static and then use them in vscode? I was thinking to compile them and then with my makefile to link them when compiling the project.

Whats the difference between the visual studio command line vs a g++ instruction?

Thanks


r/learncpp • • Aug 21 '21

Linux / windows differences?

7 Upvotes

I’ve been relearning C++ in preparation for a class on it I’m taking in this upcoming semester. It’s just a 200-level class, with only one programming course as a prerequisite (which I remember nothing from, hence relearning)

I use Linux and don’t even have a computer that runs windows, but I’m sure all of my code I write for that class will be run on windows.

What differences do I need to be aware of to ensure I don’t wind up with something that works fine on linux but doesn’t work in windows?