Posts by Mário Feroldi • 1,540 points
39 posts
-
10
votes3
answers179
viewsA: Two different parameters that an accepted function can classify as having O(n²) complexity?
The asymptotic complexity of an algorithm refers to the amount of iterations (minimum or maximum) that are made based on the amount of inputs to the algorithm. Moreover, its complexity is concerned…
-
3
votes4
answers8023
viewsA: When to use size_t?
According to this article the type serves to represent sizes in bytes of objects Most correct. The answers (up to the time of posting this one) are mistaken when they say that size_t is equivalent…
-
4
votes2
answers3679
viewsA: How can I add a character to each element of a Python string?
The most direct and simple way is to use list comprehension: result = ["opa" + item for item in list] The expression [f(x) for x in l], where f is a function that takes an argument and returns some…
-
2
votes2
answers59
viewsA: What is the usage limit of . push_back()?
Is there any mechanism I can use to know as many elements as the vector supports? Yes, std::vector::capacity returns the number of elements to which the container has currently allocated space, and…
-
4
votes1
answer193
viewsA: Why there are member initiators
Why such an appeal exists? To be able to choose the initialization members beyond standard boot. Let us use a practical example for the explanation: #include <string> using std::string; struct…
c++answered Mário Feroldi 1,540 -
2
votes1
answer73
viewsA: Data Member Initialization Question const
Why only one boot per member initializer is accepted ? Simple. Initialization of the members is complete before the constructor body starts executing. This from here: num1 = a; It is assignment, not…
c++answered Mário Feroldi 1,540 -
6
votes1
answer90
viewsA: Using template, why use the const modifier in this variable (C++)?
Static data members need to initialize outside the class declaration. This is: struct S { static int i; }; int S::i = 42; // definição This is a restriction of language, following the rule of a…
-
3
votes1
answer46
viewsA: Are there problems in declaring many noexcept functions?
noexcept is a tool with a specific purpose of allowing certain optimizations, which would not be possible if exceptions had to be taken into account. The first optimization that comes into my head…
-
9
votes3
answers485
viewsA: C++ - What’s the difference of using fixed C++ and typedef types of a type?
It seems that the question boils down to "what are the advantages of using fixed types of the standard library (e. g., std::uint8_t) instead of non-fixed types (e. g., unsigned char)." Well, the…
-
1
votes2
answers218
viewsA: How to go through a set with entered data?
To browse any container from the standard C++ library, you can use range-based for: for (auto elemento : contêiner) faz_algo(elemento); In your case, the container in question is a std::set. The…
-
1
votes1
answer174
viewsA: How to fix the error: "declaration of template Parameter’T' Shadows template Parameter" in C++?
The error says that the members' template parameter has the same name as the class, thus preventing its use within the member. Rename the parameter to get rid of the error: template<typename U,…
c++answered Mário Feroldi 1,540 -
2
votes1
answer68
viewsA: How to use a template as a type in a map?
c++17 introduced the type std::any, a container capable of storing objects of any kind*. Examples: #include <any> #include <string> int main() { std::any a = 42; // int a = 3.14f; //…
-
5
votes2
answers595
viewsA: "Operator" square brackets [] when creating the arrangement in C
I’ll use the latest draft N2176 (that I found here: http://www.iso-9899.info/wiki/The_Standard) of C standardization to respond. In the section [6.7.6], one of the parts of the syntax of a…
-
3
votes2
answers90
viewsA: Why can normal functions not have "auto" arguments if "lambda Expressions" can in C++?
First, auto for standard parameters exists to cover the fact that there is no way to specify generic types using template. But that was up to c++17, for that changed in c++20, with the possibility…
-
0
votes1
answer47
viewsA: How do I place strings like 'char32_t' and 'char16_t' on the console in C++?
The way for now is to use the classes std::codecvt_utf8_utf16 and std::wstring_convert (although discontinued in c++17) to convert a string to UTF-16 for UTF-8: #include <iostream> #include…
-
1
votes1
answer52
viewsA: decltype and pointers
The rules of the specifier decltype are here: https://en.cppreference.com/w/cpp/language/decltype Your case falls into any expression (third item), because *p is not a id-Expression, neither a class…
c++answered Mário Feroldi 1,540 -
2
votes2
answers217
viewsA: Create c++ directories with Linux
From c++17, it is possible to perform operations with the file system in a portable/cross-platform way, using the library <filesystem>: #include <filesystem> namespace fs =…
-
1
votes1
answer115
viewsA: Error with Template
The entity definition of a template must be on the same translation unit where it is used. Separation of declaration into headers and definition into translation units does not work with templates.…
-
4
votes2
answers111
viewsA: Should I avoid repeated access to the same method within a loop?
Instead of trying to guess, why don’t you take a look? Compiling the following code: #include <utility> #include <iostream> struct Window { std::pair<int, int> pos;…
-
2
votes2
answers774
viewsA: How to randomize an Array in c++
The algorithm std::shuffle of <algorithm> does exactly what you describe: template<typename RandomIt, typename Gen> void shuffle(RandomIt first, RandomIt last, Gen&& g) { using…
-
4
votes1
answer220
viewsA: What is raw() at ncurses. h
The functions raw() and noraw() turns on and off the raw mode terminal. That is, when the terminal is in raw mode (we can translate to "raw mode", or "no processing mode"), typed characters are…
-
1
votes1
answer158
viewsA: Problem in memory displacement
Accessing memory after being out of focus, or trying to move the same memory twice, results in undefined behavior. Your membership function BinaryTree::destroy expecting a NodeTree * as argument,…
-
8
votes2
answers391
viewsA: C++ (basic): for, references and syntax
To answer the first question, we can start with the second question, which will make the reasons for the first question more obvious. The construction (called the range-based for, or is based on…
-
1
votes1
answer104
viewsA: Problem in Binary Tree implementation
i can create the no and assign a value to its value variable, but when trying to print the tree nothing appears, the root pointer always ends up pointing to NULL. The problem is that this part of…
c++answered Mário Feroldi 1,540 -
2
votes1
answer245
viewsA: How do I add a value to a struct vector, for example in some vector field, because it is from a struct
There are some misconceptions in your code, which probably comes from your knowledge about arrays. The container std::vector grows and decreases dynamically, unlike arrays, that have a fixed size…
-
3
votes2
answers134
viewsA: What is the problem of returning a local variable?
This statement is old and exists precisely because the compilers did not know how to optimize the code well in the past, resulting in an executable with questionable performance (the variable is…
-
1
votes3
answers95
viewsA: Identify a specific word in a C++ String, is there a function ready for that? Or just in the nail itself?
There are some ways to do this, one of them being with std::string::find_last_of and std::string::substr: finding the position of space after Depende: and creating a substring that starts from that…
-
0
votes1
answer51
viewsA: List with 3 c++ elements
No, it is not possible to modify the standard library. Anything of the kind is prohibited. You can, however, have your string variable as part of the container element: struct Conteudo { std::string…
-
2
votes2
answers75
viewsA: How do I define Operator= for a c++ struct?
If you just want to copy the values without doing anything else, and assuming c++11, you can just do PATH &operator=(const PATH &) = default; inside the struct, without needing to define a…
-
1
votes1
answer1101
viewsA: Maximum possible size to allocate a vector
Utilize std::vector::max_size, that function as a member of std::vector returns the maximum number of elements that a std::vector can handle a specific system or implementation. For example (see…
-
5
votes1
answer607
viewsA: Doubt with Random in c++14 and 17
Since c++11, the library for random number generation and distribution <random> was introduced to solve this big hole that existed in c++ previously, as you have already noticed. cppreference…
-
3
votes2
answers1064
viewsA: Lambda functions in C++, when to use and what are the advantages?
Lambdas solve a problem of readability, expressiveness and practicality. For example: before c++11, if you wanted to pass a predicate to some algorithm of stl, there are a few options: Pass a…
-
1
votes2
answers323
viewsA: How to isolate higher order bits and lower order bits in C/C++?
Use the bitwise logical operators to get the bits you want. The idea is to use a mask number and apply it to a value to extract only the desirable bits (which are described in the mask). uint16_t…
-
1
votes1
answer292
viewsA: Error passing a method to another method. C++
The guy from Txt::print is not void (*)(), and yes void (Txt:: *)(). Change the type of print in class Info2: class Info2 { public: int tam; void (Txt:: *print)(); }; And assign the address of the…
c++answered Mário Feroldi 1,540 -
6
votes3
answers540
viewsA: What kind of smart pointer to choose?
What’s the difference between them? Each of the smart pointers provides a variation of the ownership rules of the resource being manipulated. For example, an intelligent pointer that has ownership…
-
0
votes1
answer49
viewsA: Debug vs Release in Cmake
CMAKE_BUILD_TYPE is the Cmake variable you are looking for. You can generate build files by passing the setting of this variable per command line: cmake -DCMAKE_BUILD_TYPE=Debug .. You can also…
-
0
votes1
answer200
viewsA: Link library boost with cmake?
You can use the module Findboost, which comes in the standard Cmake installation to include boost in your project. The syntax is as follows:: find_package(Boost [version] [EXACT] # Versão mínima ou…
-
5
votes4
answers4494
viewsA: What is a Static function for?
What’s a keyword function for static? static has different effects depending on your use and context. It can be found in: definition of functions, global and local variables, data members and member…
-
3
votes2
answers1067
viewsA: How to get the length of a char array?
It is possible to know the size of an array passed as an argument to a function using templates and type deduction: template <size_t N> void imprimeMenu(const char *(&menuOpc)[N]) { int…