Sachin
?
Nameful
how to print first 3 values of map in c++ ?
What is the "first value" in a map?
Sachin
1 2 3 5 8 9
Sachin
for(auto it= mp.begin(); it!=mp.begin()+3; it++)
Sachin
it is giving error
Nameful
Sachin
error: no match for ‘operator+’ (operand types are ‘std::map::iterator’ {aka ‘std::_Rb_tree_iterator >’} and ‘int’)
Nameful
std::next(mp.begin(), 3) I think
Nameful
how to use it ?
for(auto it = mp.begin(); it! == std::next(mp.begin(), 3); it++)
Sachin
ok thank u
Nameful
ok thank u
I fixed a minor typo, make sure you get the latest thing I typed
Sachin
ok
Anonymous
yep
Does codeforces show other's soln?
Anonymous
yes
where?
Anonymous
can you show in ss pls
?
I believe you are coming from python or other languages that uses dynamic typing. C++ is statically typed like most other compiled languages. It is useful since it prevents auto X(auto x){ return x; } Where we dont know if x is a char or an unsigned integer or an integer or a float etc... But, you can use lambda functions to achieve the same Or this is legal in C++ auto f = [](int x) { return x + 3; }; Provided x + 3 doesnt result in ambiguity. Is it recommended? I would say no when you are doing large projects. You can search Wikipedia on type system and static type.
thanks J. yes i come from javaScript to C. so why can't the compiler figure out the type of the data(how much space to be allocated in computer memory ?, in what format the data is stored in computer memory) by just the "literal/constant" appearing in a function after "return" in a return statement ? or why can't the compiler figure out the data type of a "variable declaration"(suppose that we initialize the variable at the same time we declare it) by the literal/constant appearing when we initialize the variable or why not figure out the data type in an assignment expression by just the literal/constant appearing in it. what benefits static typing has over dynamic typing ? yes i did read about them(type system, static typing, dynamic typing on wikipedia) but what good/benefits can a static typed language offer us over a dynamically typed language. MANY THANKS AGAIN
?
@SilhouetteInDark please look into my question above when you have time or when you are free. THANKS
\Device\NUL
thanks J. yes i come from javaScript to C. so why can't the compiler figure out the type of the data(how much space to be allocated in computer memory ?, in what format the data is stored in computer memory) by just the "literal/constant" appearing in a function after "return" in a return statement ? or why can't the compiler figure out the data type of a "variable declaration"(suppose that we initialize the variable at the same time we declare it) by the literal/constant appearing when we initialize the variable or why not figure out the data type in an assignment expression by just the literal/constant appearing in it. what benefits static typing has over dynamic typing ? yes i did read about them(type system, static typing, dynamic typing on wikipedia) but what good/benefits can a static typed language offer us over a dynamically typed language. MANY THANKS AGAIN
> why can't the compiler figure out the data type of a "variable declaration" auto data type exist in C++ I don't like let the compiler choose own data type. With having access to variable data types we can optimize it for size and speed. Let's take an example, memory holds value and they're literally integers. There's no different with char, short int, int, long int', `long long int beside their sizes. p = 32; You want to use p again to hold another integer that larger than before but the compiler had automatically set its type to char . What would happen if p used to hold larger integer ? An integer overflow will happen. Size problem It shouldn't matter if you have large memory but it's matter if you work under low memory. auto p = 255 printf("%zu\n", p) The size of p is 4 bytes, but actually we can reduce its size into 1 bytes by changing it into unsigned char . Another thing, when you declare a data, you need to choose size or performance. For example in 64 bits machine int is 32 bit but int_fast32_t is 64 bit typedef long int. (I forgot where i readed it) Processor bits data size is the fastest data type
Keerthiragavan
hi guys which complier is best in c++ ?
Null
g++
\Device\NUL
Anonymous
Guys can someone help me with this. Im having trouble with my "if else" statement, i already do some research in google but i cant understand
Anonymous
Guys can someone help me with this. Im having trouble with my "if else" statement, i already do some research in google but i cant understand
How can anyone help you if all you provided is that you are having problems with your if/else statements?
Anonymous
Show some code or something
Anonymous
How can anyone help you if all you provided is that you are having problems with your if/else statements?
int main(){ string name; char workingtime; double rph; float bp; double gp; int hw; int ot; int f; int p; cout<< "Enter Employee name:"; cin>> name; cout<< "Press f for Full-time or p for Part-time:"; cin>> workingtime; cout<<endl; if (workingtime == f){ cout<< "Full time Employee\n"<< endl<<"Enter basic pay:\n"; cin>> bp; cout<<endl; cout<< "Emloyee name:"<< name<< "\n"; cout<< "Gross pay:"<< bp; } else if (workingtime == p){ cout<< "Part time Employee\n"; cout<< "Enter rate per hour:"; cin>> rph; cout<< "Enter no. of hours worked:"; cin>> hw; cout<< "Enter no. of overtime:"; cin>> ot; cout<< "Employee name:"<< name; cout<< "Basic pay:"<< hw * rph; cout<< "Overtime pay:"<< ot*rph*1.5; cout<< "Grosspay:"<< bp+ot; } else printf("invalid input"); return 0; } here, sorry if my code was bad I'm new to this and I'm just trying to explore in this. by the way I'm trying to make a gross payroll that will need to input name and if full-time or part-time. also sorry if my English is bad hehehe
\Device\NUL
'f' is char while f is data. yet you compare it with f and f isn't uninitialized
Anonymous
'f' with f is different
Yup i get it a while ago ☺️ but thanks for answering my question
MᏫᎻᎯᎷᎷᎬᎠ
.
Anonymous
// finding the distance between two points #include <iostream> #include <iomanip> #include <cmath> using namespace std; class point2; class point1 { int a, b; public: point1(int, int); friend void add(point1, point2); }; class point2 { int x, y; public: point2(int, int); friend void add(point1, point2); }; void add(point1 p1, point2 p2) { cout << " the distance between two point is " << sqrt(((p1.a - p2.x) * (p1.a - p2.x)) + ((p1.b - p2.y) * (p1.b - p2.y))); } point1 ::point1(int x, int y) { cout << " enter point2 x : "; cin >> x; a = x; cout << " enter point2 y : "; cin >> y; b = y; } point2 ::point2(int a, int b) { cout << " enter point1 a : "; cin >> a; x = a; cout << " enter point1 b : "; cin >> b; y = b; } int main() { point1 a1(); point2 a2(); cout << endl; add(a1(), a2()); cout << endl; cout << endl << " Hello Peter :) "; }
Anonymous
error: undefined reference a1 and a2 ;
anko
How can i extract student full name Like: John Snow Acci to J.S.Acci in C Program I try to implement this Program but fails several time, any pro with ideas please help.
klimi
do it on paper in atomic steps and the write it in code
anko
do it on paper in atomic steps and the write it in code
OK this are Hints: Can you do somethings 1. Start from the end. 2. Check for the last word. There will be a space before the first character of the last word or the first character will also be the first character of the string if the string contains only one word. 3. Copy this last word in a new string. 4. Repeat step b) for the middle word but this time only insert the first character and a dot(.) instead of the whole word in the new string. 5. Repeat till the loop ends by encountering the first character of the string.
Anonymous
thanks J. yes i come from javaScript to C. so why can't the compiler figure out the type of the data(how much space to be allocated in computer memory ?, in what format the data is stored in computer memory) by just the "literal/constant" appearing in a function after "return" in a return statement ? or why can't the compiler figure out the data type of a "variable declaration"(suppose that we initialize the variable at the same time we declare it) by the literal/constant appearing when we initialize the variable or why not figure out the data type in an assignment expression by just the literal/constant appearing in it. what benefits static typing has over dynamic typing ? yes i did read about them(type system, static typing, dynamic typing on wikipedia) but what good/benefits can a static typed language offer us over a dynamically typed language. MANY THANKS AGAIN
The benefits of a static typed language over a dynamically typed language is safety and speed. A static typed language knows the data types of all variables and can accordingly optimize access to it. As a simple example consider data types in PHP or even Python. When a variable is encountered, Python can't immediately start processing it. It uses an indirect mechanism like a pointer to some other structure which tell it what the data type of that object currently is and only then starts processing it. All these add up and make the language slower than statically typed language. To understand this more just search Google for "why numpy is faster than python lists".
ᴹᵒʰᵃᵐᵐᵃᵈ
Hello I wrote the list of neighbors in the file, I want to give the c outline of the origin and destination to say whether there is a ridge between them, in your opinion, what is the best way to understand whether there is a ridge between them or not? I wrote the list of neighbors like this: 0: [20, 26, 2, 19, 28, 25, 14, 39, 23, 21, 43, 17, 24, 0, 11, 13, 15, 12, 18, 44, 16, 22] 1: [20, 26, 19, 28, 39, 14, 23, 10, 21, 43, 17, 53, 24, 0, 11, 13, 33, 15, 12, 18, 44, 29, 16, 22, 6] 2: [4, 26, 58, 3, 9, 1, 44, 6, 2]
Clash
//An input contains 2 integers A and B. //Print a wrong answer of A-B. Your answer must be //a positive integer containing the same number of //digits as the correct answer, and exactly one digit // must differ from the correct answer. Leading zeros //are not allowed #include <iostream> using namespace std; int main() { int a,b; cin>>a>>b; int result=a-b; if((result%2)==0) cout<<result+1<<endl; else cout<<result-1<<endl; return 0; } there is some exception test cases,can anyone help me
Clash
What if a = b
there is a constraints 1 ≤ B < A ≤ 10000
Clash
What if a-b is 1?
int main() { int a,b; cin>>a>>b; int result=a-b; if(result==1) cout<<"0"<<endl; else if((result%2)==0) cout<<result+1<<endl; else cout<<result-1<<endl; return 0; } even this code also not passing test cases
Anonymous
Are soring algorithms worth learning these days
Anonymous
where everything is basically done by AI
Leovan
SOIL library is dead? I can't find their website/ docs
%Nikita
Hi, guys! I am kinda confused of this piece on C11: #include <stdbool.h> const char* bool_to_str (bool arg){ return (const char*[]){“false”, “true”}[(int)arg]; } Are there some memory leaks while returning string literal or not? Returning string like this: return “I am sus string”; Looks interesting, but it is just a char pointer returning. What happened with memory? Is it forbidden?
%Nikita
SOIL library is dead? I can't find their website/ docs
Did you mean this library? https://github.com/littlstar/soil
Rodrigo
hello, does anybody have a good info on the situation of the C++ market in 2022? That is, how popular the language is nowadays and which areas usually use it. I'm completely new on this language
NIGHT
I need helps in C++
NIGHT
can someone help me please it's urgent
🙃🙂
Why printf and scanf showing error in CLI visual studio 2019
.....
insited of conio.h , what can i used in mac os ?
.....
okay
Álvaro
Hi, has somebody has done some done for CUDA?
J
hello, does anybody have a good info on the situation of the C++ market in 2022? That is, how popular the language is nowadays and which areas usually use it. I'm completely new on this language
Microcontrollers(eg: Arduino, etc...), OS development(Recently, Fucshia, Serenity, etc... with honorable mentions, Microsoft and MacOS which have many parts in C++), Web Backends(For now, I can think of only Telegram), Game Development(Core of Unity, Unreal etc... and is used in UE for scriting), Data Science(ML, Deep Learning etc...)(Tensor flow and Pytorch uses C++ at its core), Emulation(yuzu, etc...), Audio, Web Browsers(Chromium and Firefox), Certain GUI libraries like Skia, part of Flutter etc..., you name it
J
Firefox is increasingly switching over to Rust
*Integrating Rust into C++ code base* and not rewriting Firefox into Rust(eg: Servo). It includes some parts where Memory safety is crucial.
J
It is being done in many C++ programs
Anonymous
I want learn c programming
%Nikita
bool_to_str are not in Standard C11 It is stored on data segment, cmiiw Try char *ptr = "aaa" ptr[0] = 'a' You will get RunTime Error In C++11 You need to implicitly define char * as const.
I know. It is my function, just to show my problem. I really don’t understand why char* ptr = "aaa"; ptr[0] = ‘a’ Not working, but: char* ptr = “aaa”; putchar(*ptr); Gives me a right result ‘a’; Wtf I can read from it, but I can’t write to it. With array char ptr[] = “aaa”; works! Why I can’t write to it, just because it is pointer, and it is not const. With malloc: char* ptr = malloc(4); memcpy(ptr, “aaa”, 4); ptr[0] = ‘a’; free(ptr); It works! Why it works? Why it doesn’t work with pointer init by string literal? Mystery C
%Nikita
well u did not initialize the string using malloc in your first and second code
You are right. It is because I haven’t to do it, when I initialize it with string literal. If I write char* str = “some str”; then it will malloc by compiler, like this: int a; I don’t really need to malloc it, compiler does all work
%Nikita
But the question is why I can’t rewrite this string literal, but can read
Anonymous
But the question is why I can’t rewrite this string literal, but can read
So when u try to write it, does it return an error?
%Nikita
It returns segmentation fault
%Nikita
If I pass it to function like this: void f(char* s){ s[0] = ‘a’; } … char* literal = "aaa"; f(literal); There is no seg fault
Anonymous
I guess that's bcs when u init it by string literal,it's just a constant that u can't write. However, with memcpy(), it'll be seen as an array or so? I don't know what exactly happens.
olli
If I pass it to function like this: void f(char* s){ s[0] = ‘a’; } … char* literal = "aaa"; f(literal); There is no seg fault
depending on your compiler and platform this will still seg fault. It is undefined behavior either way. But yes, as already stated you cannot change a string literal. In the code below`A` and B are not the same, one is an array with size 12 and the other one is just a pointer. char A[] = "hello world"; char *B = "hello world";
\Device\NUL
pointer of chars are stored in data segment not in heap