#strings basic
1 messages · Page 1 of 1 (latest)
<@&987246683568103514> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
what strings are u talking about in c++? std::string or char*?
unlike java, in c++, strings are char arrays already. std::string is merely a convenience wrapper around the raw char array
std::string has a method called c_str() which gives u the underlying raw char array
note that, unlike java, strings are mutable in c++. so be careful when u edit it
and note that the array is one larger than the actual string length, since strings in c++ (usually) end with the nul-terminator
Yes i mean std::string
in 2nd line so you mean
string s = "dsd";
and
char s[] = {'d','s','d'};
are both same in cpp ??
2.) another qn --- are heap and binary heap same thing ?
or they are different
they are very similar. std::string is just a convenience wrapper around latter
for ease of use
capability-wise, the biggest difference is probably that std::string is still aware of the length
while raw arrays are not
never heard "binary heap"
id suggest u share the source where u got this from. im sure it makes sense then
got it 👍
so raw array we can do array.size()
what do you mean by they are not aware of the length
so raw array we can do array.size()
no. c-style arrays dont know their size
there is no such method
ohh yeah now i understood, in another function they do not know their size
we have to pass the size separately
but in int main we can do it, right ?
not sure what u mean. arrays dont know their size. fullstop
hmm now i got it, i just got confused
thanks for solving the questions
public char[] toCharArray()
Converts this string to a new character array.
You can use this if u want toconvert to char array
Oh nvm ur question itself uses that method
@tough kelp i made an class with a pointer in it
so some uses -->
ObjectOfThatClass->pointer;
to access it
and sometime i saw ObjectOfThatClass.pointer;
to access it, which is correct ?
foo->bar is a shorthand for (*foo).bar
so its a combination of "resolve this pointer and then access that property"
Person p1("John");
std::cout << p1.name;
Person* p2 = &p1;
std::cout << p2->name;
// same:
std::cout << (*p2).name;
oh got you
i got another doubt --->
some make the object of the class like ---
ClassName *ObjectName = new ClassName();
and some make it with
ClassName ObjectName;
what are the differences and are both correct in cpp ? where to prefer which one ? @tough kelp
Person p; is created on the stack. Person* p = new Person(); is created on the heap
sounds harmless, but the differences are significant
first of all, the stack is small in comparison (~500 MB maybe, depends on the system)
the heap is the rest of ur RAM, so typically multiple gigabytes
stack is also slightly faster than heap
code-wise, objects on the stack are auto-deleted when they go out of scope
so
...
{
Person p;
...
} // p is killed NOW
... // p is dead, cant be used anymore
objects on the heap are never auto-deleted
they keep hogging ur memory until u explicitly free them again:
...
Person* p = new Person();
...
delete p; // p is killed NOW
... // p is dead, must not be used anymore
if u forget to delete it, it will stay there and consume ur memory. thats called a memory leak
in general: prefer the stack
unless:
- u create a lot of data or dont know how much (e.g. u want the user to enter a list of names and u dont know how many they will enter)
- u want shared ownership (object should only die once nobody uses it anymore)
- u want to transfer ownership to someone else
in practice, cause its so easy to do mistakes with pointers, people dont write Person* p = new Person(), but instead use smart pointers std::unique_ptr, std::shared_ptr, std::weak_ptr. with them, u can essentially benefit from the stack-like syntax, but have the actual objects on the heap
got it, thanks for answering @tough kelp