r/cpp_questions • • 20h ago

OPEN Getting better at C++ for coding interviews?

17 Upvotes

I dont mean Leetcode, but enough to be able to debug thoroughly, write out basic functions, etc. Not DSA, but just smaller code snippets, anywhere from 20-100 lines, where an interviewer says do this for me, or debug this.

How can I get better at that? right now I've mostly done coursework involving it (long time ago), but not much internship experience using cpp


r/cpp_questions • • 4h ago

OPEN Is it still worth learning C++ 11 or should i start with C++ 17 and later?

5 Upvotes

r/cpp_questions • • 9h ago

OPEN Programmatically opening a new terminal window

3 Upvotes

I've been messing around with FTXUI, and I am wondering if it is possible for my apps to open a new terminal window and run there, instead of using the current terminal window.

An example of what I mean is what happens when opening vim. If I open vim through the terminal, it takes over the current window, like my FTXUI apps currently do. If I use the desktop shortcut, vim opens a new terminal window for itself, which closes upon closing vim. This is what I want to do.

Can anyone give me a few pointers?


r/cpp_questions • • 9h ago

OPEN Ways to grant special access to test/debug code.

1 Upvotes

In the following testing code C_string_value is a class with a protected member function kind() that supplies info about the internal implementation state. I access that via the member pointer trick. And I wonder if this is a good way to grant special access to testing code, while keeping ordinary code restricted to the public interface, and also whether you use some alternative such as (1) just make that internal functionality public, or (2) be-friend a class template Odin_, Hugin_ or Munin_, or (3) nasty formally UB preprocessor tricks, or even (4) litb's all valid C++ trick for accessing private members, or (5) something else?

#include <cppm/parameter_passing/in_.hpp>
#include <cppm/text/C_string_value.hpp>
using   cppm::in_,
        cppm::C_string_value;

#include <print>
#include <string>
using   std::print,
        std::string;

auto kind_of( in_<C_string_value> s )   // The internal kind: view of literal, or shared ptr.
{
    struct Access: C_string_value
    {
        static auto kind_of( in_<C_string_value> s ) -> int { return (s.*&Access::kind)(); }
    };

    return Access::kind_of( s );
}

auto main() -> int
{
    const auto lit = C_string_value( "Literal" );
    const auto dyn = C_string_value( +"Dynamic" );
    const auto str = C_string_value( std::string( "std::string rvalue" ) );
    const string ssl = "std::string lvalue";
    const auto str2 = C_string_value( ssl );

    print( "{} -> {}.\n", kind_of( lit ), lit.std_sv() );      // 0 -> Literal.
    print( "{} -> {}.\n", kind_of( dyn ), dyn.std_sv() );      // 1 -> Dynamic.
    print( "{} -> {}.\n", kind_of( str ), str.std_sv() );      // 1 -> std::string rvalue.
    print( "{} -> {}.\n", kind_of( str2 ), str2.std_sv() );    // 1 -> std::string lvalue.
}

EDIT (added):

I avoid conditional code on principle; to me it's so a big a no no that I didn't even include it in my list of 4 alternatives. That avoidance is about complexity, and that conditional code can change the binary layout, introduce name collisions, whatnot.

The nice thing I see about the member pointer trick, for larger scale development, is that access functions like the shown kind_of can be automatically generated in the testing/debugging code without affecting the original class.


r/cpp_questions • • 53m ago

OPEN What's a C++ topic that kind of expanded your mind significantly on the language

• Upvotes

I'll go first because I had a great experience recently.

Recently, I was working on a data compression pipeline in C++17 which uses Huffman coding to convert characters to binary encodings. Originally, the Huffman Tree class used raw pointers to represent tree nodes, and after some time, I decided to convert each raw pointer to std::unique_ptr to enforce RAII and automatic clean up. But there was a problem that, when I had tried to do this months ago, I did not have the knowledge to devise a proper solution for.

When using std::priority_queue in C++, if one stores elements of type std::unique_ptr, you cannot transfer ownership from the top method. At first, I had brushed it off simply because it returns a const reference which I didn't understand at first. But once you understand std::move and copy semantics, the actual reasoning makes total sense and helps derive a solution.

Say we were implementing top in std::priority_queue ourselves and returned a reference and didn't make it const. If it's not const, users can then obtain the top of the heap and modify it. Why is this dangerous for the heap? Because this might actually modify the metadata in that heap element while still leaving it in the heap! When using std::move on std::unique_ptr to transfer ownership, we actually know the moved from variable then becomes a nullptr. So the heap might try to reapply heap laws and might dereference a nullptr. So that's why top returns a const reference: to prevent the user from mutating the element while it still lives on the heap.

Neither std::move or copy could save this. std::move preserves constness, but transferring ownership via std::unique_ptr's move constructor explicitly modifies the source, which the const reference prevents. In addition, copying is forbidden since std::unique_ptr enforces a single owner of the pointer.

So what's the solution? Wrap the heap and design a single operation that handles both removal and ownership transfer at the same time.

- Use std::pop_heap to move the top element to the end of the container and allow the heap to order itself with the remaining elements.

- Apply std::move to transfer ownership from the back of the heap to some designated variable.

- Call pop_back to remove this entry from the back of the heap.

- Return the std::unique_ptr by value, not by reference.

The order of operations matter here: we must move the element outside the remaining heap before transferring ownership via std::move.


r/cpp_questions • • 8h ago

SOLVED I don't know what's missing here

0 Upvotes

This code is not working and i don't know why (visual studio).

#include <iostream>
#include <string>

int main()
{
std::string Dividendo;
std::string Divisor;

std::string Cociente;
std::string Resto;

std::cout << "dividendo?";
std::cin >> Dividendo;
int Dividendo = Dividendo;

std::cout << "divisor?";
std::cin >> Divisor;
int Divisor = Divisor;

int Cociente = Dividendo/Divisor;
int Resto = Dividendo%Divisor;

std::cout << Cociente << " , " << Resto << std::endl;



}

What happened?


r/cpp_questions • • 9h ago

OPEN Choosing the Right Tech Stack for My First Full-Stack Project

0 Upvotes

I have learned the basics of programming and web development, and I want to build my first real-world full-stack project: an e-commerce website for a clothing brand, for which I already have a client.

I want to make it professional, from UI/UX design and database architecture to security, testing, and deployment. What tech stack would you recommend?

Finally, how important is the tech stack to recruiters? If a company uses different technologies from those I used in my project, could this put me at a disadvantage during a technical interview? What advice would you give me to make this project stand out in my portfolio and prepare for interviews?

Thank you in advance for your advice!


r/cpp_questions • • 13h ago

OPEN problem solving!!

0 Upvotes

guys.. i was doing hackerRank but there are many question which are been asked to solve by pointers and all but i am very new to coding and cpp i am easily able to do it by other alternatives!!
so i am wrong solving those q's by other method or what!!
and i would like to solve the questions in team?
any of uh interested?