C-Plus-Plus | various algorithms in mathematics , machine learning | Learning library
kandi X-RAY | C-Plus-Plus Summary
Support
Quality
Security
License
Reuse
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample Here
C-Plus-Plus Key Features
C-Plus-Plus Examples and Code Snippets
Trending Discussions on C-Plus-Plus
Trending Discussions on C-Plus-Plus
QUESTION
I have come across a piece of example code that uses pointers and a simple subtraction to calculate the number of items in an array using C++.
I have run the code and it works but when I do the math on paper I get a different answer.
There explanation does not really show why this works and I was hoping someone could explain this too me.
#include
using namespace std;
int main() {
int array[10] = {0, 9, 1, 8, 2, 7, 3, 6, 4, 5};
int stretch = *(&array + 1) - array;
cout << "Array is consists of: " << stretch << " numbers" << endl;
cout << "Hence, Length of Array is: " << stretch;
return 0;
}
From: https://www.educba.com/c-plus-plus-length-of-array/
When I run the code I get the number 10.
When I print the results of *(&array + 1)
and array
by
cout << *(&array+1) << endl; cout << array << endl;
I get of course two hex address's.
When I subtract these hex numbers I get 1C
or 28
???
Is it possible that C++ does not actually give the hex results or their translation to decimal but rather sees these numbers as addresses and therefore only returns the number of address slots remaining?
That is the closest I can come to an explanation if some one with more knowledge than I could explain this I would be very grateful.
ANSWER
Answered 2021-Dec-06 at 01:34You forgot a small detail of how pointer addition or subtraction works. Let's start with a simple example.
int *p;
This is pointing to some integer. If, with your C++ compiler, int
s are four bytes long:
++p;
This does not increment the actual pointer value by 1, but by 4. The pointer is now pointing to the next int
. If you look at the actual pointer value, in hexadecimal, it will increase by 4, not 1.
Pointer subtraction works the same way:
int *a;
int *b;
// ...
size_t c=b-a;
If the difference in the hexadecimal values of a
and b
is 12, the result of this subtraction will not be 12, but 3.
When I subtract these hex numbers I get 1C or 28 ???
There must've been a mistake with your subtraction. Your result should be 0x28
, or 40
(most likely you asked your debugger or compiler to do the subtraction, you got the result in hexadecimal and assumed that it was decimal instead). That would be the ten int
s you were looking for.
QUESTION
Here is my menu
If I click on Market for example, the background color must be in green.
I don't understand why the Currency and the Portfolio are in green?
In fact, the green background appears depending on the menu selected.
How can I solve this problem, please?
I think that the problem is perhaps here
admin.component.html
Menu
Dashboard
TS
export class AdminComponent implements OnInit {
selectedTab!: string;
showSubmenu: any[] = [];
showInfo: any[] = [];
menus: any[] = [
/* Market */
{
class: 'bx bx-grid-alt',
item: 'Market',
route: 'market',
submenus: [
{
class: 'bx bx-box',
item: 'Item',
route: 'item',
},
],
},
/* Currency */
{
class: 'bx bx-grid-alt',
item: 'Currency',
route: 'currency',
},
/* Porfolio */
{
class: 'bx bx-box',
item: 'Portfolio',
route: 'portfolio',
submenus: [
{
class: 'bx bx-grid-alt',
item: 'Element',
route: 'element',
},
],
},
];
I share you my code here.
Thank you very much for your help.
ANSWER
Answered 2021-Dec-04 at 12:27You just need to change like this:
HERE your stackblitz corrected.
QUESTION
To keep it simple, I created a menu and all of my bullet lists are stored in an array.
My problem is that when I run the loop it shows me 2 times, the same values ?? I do not understand why?
I think that the problem is in the HTML file?
admin.component.html
Menu
Dashboard
TS
export class AdminComponent implements OnInit {
showSubmenu: any[] = [];
menus: any[] = [
{
item: 'Market',
submenus: [{ item: 'Item' }],
},
{
item: 'Portfolio',
submenus: [{ item: 'Element' }],
},
];
constructor() {}
ngOnInit() {}
toggleMenu(index: number) {
this.showSubmenu[index] = !this.showSubmenu[index];
}
}
I can not retrieve the index of each array? I can do this? If so... How, please?
https://stackblitz.com/edit/angular-ivy-9ytvbk?file=src/app/admin/admin.component.html
ANSWER
Answered 2021-Dec-03 at 22:32Please look at this solution :
https://stackblitz.com/edit/angular-ivy-1hnhqr?file=src/app/admin/admin.component.html
QUESTION
I would like to create a menu with a down arrow like this:
When I click on Market
, a submenu is shown, and the arrow is up.
I don't understand how to I can get this same result.
admin.component.html
Menu
Dashboard
admin.component.ts
export class AdminComponent implements OnInit {
constructor() {}
elementMenu: boolean = false;
selectedTab!: string;
ngOnInit() {}
selectElementMenu() {
this.elementMenu = !this.elementMenu;
}
}
The code is on Stackblitz
I thank you in advance for your help.
ANSWER
Answered 2021-Dec-02 at 13:35Simply use *ngIf to conditionally render icon
Market
QUESTION
I would like to add a down arrow for each li `at the right.
Here is an idea of the menu.
How to I create the arrow, please? I have to use an image?
I can you provide my code below:
admin.component.html
styles.css
.sidebar .nav-links {
padding: 0;
}
.sidebar .nav-links li {
position: relative;
list-style: none;
height: 50px;
}
.sidebar .nav-links li a {
height: 100%;
width: 100%;
display: flex;
align-items: center;
text-decoration: none;
transition: all 0.4s ease;
}
.sidebar .nav-links li a:hover {
background: #081d45;
}
.sidebar .nav-links li i {
min-width: 60px;
text-align: center;
font-size: 18px;
color: #fff;
}
.sidebar .nav-links li a .links_name {
color: #fff;
font-size: 15px;
font-weight: 400;
white-space: nowrap;
text-transform: uppercase;
}
.sidebar .nav-links li a:hover,
.selected {
background: #081d45;
}
You can consult my project below -> https://stackblitz.com/edit/angular-ivy-1sawka?file=src/app/admin/admin.component.html
ANSWER
Answered 2021-Dec-02 at 12:11You may use icons as font with adding to your project instead of using them separated images. I think best option for angular project is material icons.
full icon list of material icons
You may also check for font awesome.
QUESTION
I have a component named admin.component, in this component, I have a menu and a header.
In fact, when I create a new page, for example: the currency page.
The title is at the bottom of the la page, I don't understand why?
Normally, the title must be at the top...
I think that the component admin.component.html isnt't correct?
For the currency.component.html page, I have this:
Currency Page
The code is available here, if you wish.
QUESTION
I have a perfect menu in HTML/CSS
My problem is that bootstrap distorts the menu when I add the link Bootstrap.
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
.sidebar {
position: fixed;
height: 100%;
width: 240px;
background: #0a2558;
transition: all 0.5s ease;
}
.sidebar.active {
width: 60px;
}
.sidebar .logo-details {
height: 80px;
display: flex;
align-items: center;
}
.sidebar .logo-details i {
font-size: 28px;
font-weight: 500;
color: #fff;
min-width: 60px;
text-align: center;
}
.sidebar .logo-details .logo_name {
color: #fff;
font-size: 24px;
font-weight: 500;
}
.sidebar .nav-links {
margin-top: 10px;
}
.sidebar .nav-links li {
position: relative;
list-style: none;
height: 50px;
}
.sidebar .nav-links li a {
height: 100%;
width: 100%;
display: flex;
align-items: center;
text-decoration: none;
transition: all 0.4s ease;
}
.sidebar .nav-links li a.active {
background: #081d45;
}
.sidebar .nav-links li a:hover {
background: #081d45;
}
.sidebar .nav-links li i {
min-width: 60px;
text-align: center;
font-size: 18px;
color: #fff;
}
.sidebar .nav-links li a .links_name {
color: #fff;
font-size: 15px;
font-weight: 400;
white-space: nowrap;
}
.sidebar .nav-links .log_out {
position: absolute;
bottom: 0;
width: 100%;
}
HTML CSS JS
CodingLab
ANSWER
Answered 2021-Nov-27 at 21:11you can try this :
.sidebar .nav-links
{
padding: 0;
}
QUESTION
I just started trying to use shield.io badges today. I'm a little confused.
When I needed to use the C++ logo in a label I used the following request:
https://img.shields.io/badge/C++-%2300599C.svg?&style=for-the-badge&logo=c%2B%2B&logoColor=white
Which yielded a label with a logo, as intended.
I expected it to be logo=c-plus-plus
and through trial and error I eventually found out that I should use the encoding for the +
sign, %2B
.
Now I can't find the logo for Selenium, although it is clearly on the simple-vector
list (https://simpleicons.org/icons/selenium.svg) and there's nothing odd about this name, no spaces or signs, so I don't know how to find it. logo=selenium
should work but it doesn't.
This is what I'm trying: https://img.shields.io/badge/selenium-%2343B02A.svg?&style=for-the-badge&logo=selenium&logoColor=white
And what I get is a label without logo. Any thoughts?
PS: I don't have enough reputation to post images so I can't include the resulting images in the text but you can copy the link to see what I mean.
ANSWER
Answered 2021-Feb-12 at 12:28Somehow it works now and I haven't changed anything in the code. Yay (?)
This will probably happen again.
QUESTION
Functional Programming in C++, at page 214, with reference to an expected
monad which is the same as Haskell's Either
, reads
[...] as soon as any of the functions you're binding to returns an error, the execution will stop and return that error to the caller.
Then, in a caption just below, it reads
If you call
mbind
[equivalent to Haskell's>>=
] on anexpected
that contains an error,,mbind
won't even invoke the transformation function; it will just forward that error to the result.
which seems to "adjust" what was written earlier. (I'm pretty sure that either LYAH or RWH underlines somewhere that there's no short-circuiting; if you remember where, please, remind me about it.)
Indeed, my understanding, from Haskell, is that in a chain of monadic bindings, all of the bindings happen for real; then what they do with the function passed to them as a second argument, is up to the specific monad.
In the case of Maybe
and Either
, when the bindings are passed a Nothing
or Left x
argument, then the second argument is ignored.
Still, in this specific two cases, I wonder if there's a performance penalty in doing something like this
justPlus1 = Just . (+1)
turnToNothing = const Nothing
Just 3 >>= turnToNothing >>= justPlus1 >>= justPlus1 >>= justPlus1 >>= justPlus1 >>= justPlus1
as in these cases the chain can't really do anything other than what it does, given that
Nothing >>= _ = Nothing
Left l >>= _ = Left l
ANSWER
Answered 2020-Sep-17 at 19:32Consider the following expression:
result :: Maybe Int
result = x >>= f >>= g >>= h
In that expression, of course, x :: Maybe a
for some a
, and each of f
, g
, and h
are functions, with h
returning Maybe Int
but the intermediate types of the pipeline could be anything wrapped in a Maybe
. Perhaps f :: String -> Maybe String
, g :: String -> Maybe Char
, h :: Char -> Maybe Int
.
Let's make the associativity explicit as well:
result :: Maybe Int
result = ((x >>= f) >>= g) >>= h
To evaluate the expression, each bind (>>=
) must in fact be called, but not necessarily the functions f
, g
, or h
. Ultimately the bind to h
needs to inspect its left-hand argument to decide whether it is Nothing
or Just something
; in order to determine that we need to call the bind to g
, and to decide that we need to call the bind to f
, which must at least look at x
. But once any one of these binds produces Nothing
, we pay only for checking Nothing
at each step (very cheap), and not for calling the (possibly expensive) downstream functions.
Suppose that x = Nothing
. Then the bind to f
inspects that, sees Nothing
, and doesn't bother to call f
at all. But we still need to bind the result of that in order to know if it's Nothing
or not. And this continues down the chain until finally we get result = Nothing
, having called >>=
three times but none of the functions f
, g
, or h
.
Either
behaves similarly with Left
values, and other monads may have different behaviors. A list may call each function one time, many times, or no times; the tuple monad calls each function exactly once, having no short-circuiting or other multiplicity features.
QUESTION
In Functional programming in C++, chapter 11 deals with some basic template meta programming.
In this context, the author shows this implementation of remove_reference
/remove_reference_t
, which are essentially the same as those described on cppreference.
template struct remove_reference { using type = T; };
template struct remove_reference { using type = T; };
template struct remove_reference { using type = T; };
template using remove_reference_t = typename remove_reference::type;
With reference to the code above, the author comments that when "calling" remove_reference_t
, only the general (or primary? What is the correcto word here?) template successfully substitutes T
, and the other two fail. This is clear to me, there's no way int
can be written as/matched against T&
or T&&
.
As regards remove_reference_t
, however, the author says that the second specialization cannot match. Well, couldn't it be a match thanks to reference collapsing? I mean, can't T&&
match int&
if I substitute T
for int&
, thus getting int&&& == int&
?
Similarly, when calling remove_reference_t
, can't the first specialization's T&
match int&&
if T
is substituted for int&
? (Why in the world did I think that & &
would collapse to &&
instead of &
?)
What makes the compiler discard one specialization?
ANSWER
Answered 2020-Sep-08 at 22:27only the general (or primary? What is the correcto word here?) template
The technical term used by the C++ Standard is "primary class template". It will also be the most general class template, compared to its partial specializations and explicit specializations. So that could also be a reasonable thing to call it, given enough context.
The "reference collapsing rule" is found in [dcl.ref]/6 and applies mainly when determining the meaning of combining a specific type name which aliases a reference type with a &
or &&
token which would normally form a reference to the type name's type. Deducing template arguments for a template parameter of the form T&
or T&&
is sort of the reverse of that. Although it's helpful to think of template argument deduction as "find the template arguments so that the resulting types match up", the technical details of template argument deduction are much more specific; [temp.deduct] is several pages of rules for exactly how this deduction proceeds, and there are additional relevant rules in other sections. The detail is needed so compilers agree on cases when there could otherwise be more than one "correct" answer, and so that compilers aren't required to deal with some of the more difficult cases.
In particular, when matching a dependent type P
with a known type A
, by the list of deducible types in [temp.deduct.type]/8, deduction can occur if both P
and A
have the form T&
or if both have the form T&&
. When attempting argument deduction for the partial specialization remove_reference
to determine the definition of remove_reference
, P
is T&&
and A
is int&
, so they do not share one of these forms.
The template argument deduction rules do not have a general allowance for deducing arguments from a reverse of the reference collapsing rule. But they do have a limited allowance which is related for certain cases: Per [temp.deduct.call]/3, if T
is a template type parameter, but not a parameter for a class template, then the type T&&
is a forwarding reference. When comparing types for argument deduction, if P=T&&
is a forwarding reference type and A
is an lvalue reference type, then the template type parameter T
can be deduced as the lvalue reference type A
, only if A
is the type of an lvalue function argument expression ([temp.deduct.call]/3 again) or sometimes if P
and A
are being compared because they represent function parameter types within two compared function types ([temp.deduct.type]/10).
Similarly, when ["]calling["]
remove_reference_t
, can't the first specialization'sT&
matchint&&
ifT
is substituted forT&
?
In this case, there's no possible way that the partial specialization remove_reference
can match remove_reference
. Even if the process of template argument deduction allowed finding a potential answer for this case, there is no possible type T
such that T&
is the same as int&&
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install C-Plus-Plus
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page