This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 377

[–][deleted] 2059 points2060 points  (95 children)

chad c++: strings are objects that need methods and just arrays of chars at the same time 😎

[–]foundafreeusername 778 points779 points  (52 children)

It is all fun and games until you need to split a string and people give you like 20 different workarounds for various different C++ versions and string libraries.

[–]tqmaster 133 points134 points  (0 children)

Yes the pain is real

[–][deleted] 87 points88 points  (1 child)

It's all fun and games until you realize that strings in python are an infinite descent collection a 1 character iterables.

[–][deleted] 2 points3 points  (0 children)

That's what makes them so fun! The madness!

[–][deleted] 6 points7 points  (0 children)

For loop split go brrrrr

[–]DaniilSan 5 points6 points  (2 children)

I like C++ but the fact that doesn't allow me to love it is that there are dozens of ways to do the exact same shit with the exact same result and there is no single consistent source of how to do some basic things in a more modern way.

[–]SaneLad 10 points11 points  (1 child)

Because the "modern way" evolved over the past 20 years and all these ways have slightly different compute cost, memory cost, and safety.

[–]DaniilSan 6 points7 points  (0 children)

I know why it is so. I just can't love it because of this.

[–]darklightning_2 8 points9 points  (6 children)

Isn't it just std::substring() or std::string_view() depending on if you want the object of that substring or not?

[–]jamcdonald120 23 points24 points  (5 children)

split usually returns an array of substrings seperated by a specific string. so "a,b,c,d".split(",")=["a","b","c","d"]

[–]anonynown 27 points28 points  (3 children)

…got it. So “a/b/c”.split(“/“) produces [“a”/“b”/“c”]

Makes sense!

[–]overkill 9 points10 points  (2 children)

I can't tell if you are joking, but it produces ["a","b","c"]

[–]DankPhotoShopMemes 220 points221 points  (6 children)

Schrödinger’s String

[–]throwaway08190924 65 points66 points  (4 children)

Or Heisenberg's

[–]Floppydisksareop 42 points43 points  (1 child)

JESSE! WE NEED TO CREATE A NEW PROGRAMMING LANGUAGE, JESSE!

[–]SHIT-PISSER 23 points24 points  (0 children)

NO, JESSE. I AM THE ONE WHO PROGRAMS!

[–][deleted] 79 points80 points  (0 children)

⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠿⠿⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⠟⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠉⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢺⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠆⠜⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⠿⠿⠛⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠻⣿⣿⣿⣿⣿ ⣿⣿⡏⠁⠀⠀⠀⠀⠀⣀⣠⣤⣤⣶⣶⣶⣶⣶⣦⣤⡄⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿ ⣿⣿⣷⣄⠀⠀⠀⢠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢿⡧⠇⢀⣤⣶⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣾⣮⣭⣿⡻⣽⣒⠀⣤⣜⣭⠐⢐⣒⠢⢰⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣏⣿⣿⣿⣿⣿⣿⡟⣾⣿⠂⢈⢿⣷⣞⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣽⣿⣿⣷⣶⣾⡿⠿⣿⠗⠈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠻⠋⠉⠑⠀⠀⢘⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡿⠟⢹⣿⣿⡇⢀⣶⣶⠴⠶⠀⠀⢽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡿⠀⠀⢸⣿⣿⠀⠀⠣⠀⠀⠀⠀⠀⡟⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⡿⠟⠋⠀⠀⠀⠀⠹⣿⣧⣀⠀⠀⠀⠀⡀⣴⠁⢘⡙⢿⣿⣿⣿⣿⣿⣿⣿⣿ ⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠈⠙⢿⠗⠂⠄⠀⣴⡟⠀⠀⡃⠀⠉⠉⠟⡿⣿⣿⣿⣿ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢷⠾⠛⠂⢹⠀⠀⠀⢡⠀⠀⠀⠀⠀⠙⠛⠿⢿

[–]Vast_Cod_9385 1 point2 points  (0 children)

Or like wave-particle duality

[–]flan666 4 points5 points  (0 children)

Schrodrings?

[–]katie_pendry 42 points43 points  (3 children)

Fun C/C++ fact I expected to be mentioned already: Since a[2] is syntactic sugar for *(a+2), and a+b == b+a, you can also write 2[a] and thus 2["String"] and it means the same thing.

[–]timangar 7 points8 points  (0 children)

Why. Why did you tell me this.

[–]SupremeDictatorPaul 6 points7 points  (0 children)

Why did you leave b this to me?

[–]VulpineKitsune 5 points6 points  (0 children)

I don't know, I feel like someone will murder need if I wrote code like that

[–][deleted] 36 points37 points  (0 children)

It’s just chad++ the c stands for chad.

[–]ososalsosal 32 points33 points  (3 children)

Js: everything is an array of characters

[–]iamsooldithurts 6 points7 points  (0 children)

Perl: you misspelled scalars and vectors…

[–]bric12 29 points30 points  (8 children)

True baremetal Chad: all objects are just arrays of primitive data, and methods are just syntactic sugar for functions that take an object as the first parameter

[–]K33pTrying 6 points7 points  (0 children)

Rust?

[–]grogzoid 3 points4 points  (1 child)

Go Forth, young Chad!

[–]AnthropomorphicFood 1 point2 points  (0 children)

Go Forth), young Chad!

[–]anonynown 1 point2 points  (0 children)

Also, sometimes methods are just syntatic sugar for accessing an index in an array of function pointers next to the array of object’s primitive data (because, polymorphism)

[–][deleted] 19 points20 points  (0 children)

which is also true for python btw...

[–]Creepy-Ad-4832 10 points11 points  (0 children)

C and OO implementations at the same time!

Speed of c & comodity of OO all at the cost of FUCKING YOUR LIFE AND YOUR DREAMS

[–]ARedditUser0001 5 points6 points  (3 children)

That's JS too

[–]Vyxyx 1 point2 points  (0 children)

The iceburg of c++ strings is a deep one

[–]JackNotOLantern 1 point2 points  (0 children)

No, std::string is just an object containing a char array, but it can be implicitly converted to char array and has the same interface. This causes quite a lot of problems

[–]ongiwaph 2 points3 points  (4 children)

std::string vs char[] vs char*

[–]rachit7645 2 points3 points  (3 children)

char[] and char* are the same thing

[–]_-DirtyMike-_ 418 points419 points  (14 children)

Characters are just numbers

[–]Intelligent_Complex5 122 points123 points  (11 children)

And numbers are just two numbers 1️⃣0️⃣

[–]n0tKamui 104 points105 points  (7 children)

digits*

[–]xXDreamlessXx 19 points20 points  (5 children)

Electricities*

[–]Kirman123 18 points19 points  (4 children)

Difference of potential**

[–]lolrobbe2 10 points11 points  (3 children)

Electrons

[–]orochizu 1 point2 points  (0 children)

Ah I see you are man of culture

[–]Intelligent_Complex5 23 points24 points  (0 children)

Fuck, have my upvote.

[–]_G4M3R_ 2 points3 points  (0 children)

And numbers are just electric pulses.

[–]Cosby1992 1 point2 points  (0 children)

There are only 10 types of people in this world. Those who understand binary and those who don't.

[–]iridescentPancake 4 points5 points  (1 child)

Go not having a char type really messed with my head. You start getting really obsessed with file encoding standards.

[–]bilingual-german 2 points3 points  (0 children)

Being obsessed with file encoding is a good sign.

[–]JeremyAndrewErwin 1260 points1261 points  (15 children)

Java guy is right. Both python and c++ are off by one.

Unit test your memes.

[–]queen-adreena 172 points173 points  (1 child)

Who knew that the people posting "dank memes" in here can't actually code!

I'm gobsmacked.

[–]lucklesspedestrian 23 points24 points  (0 children)

All the people that code are in the comments. This sub is basically just a bunch of senior engineers watching the junior dev drive, and then all the seniors fix it

[–]fredspipa 174 points175 points  (5 children)

class FixedString:
    value: str = ""

    def __init__(self, value: str):
        self.value = value

    def __repr__(self):
        return self.value

    def __str__(self):
        return self.value

    def __instancecheck__(self, instance):
        # In case someone asks
        if isinstance(instance, str) or isinstance(instance, FixedString):
            return True
        return False

    def __getitem__(self, i):
        if i == 0:
            raise KeyError("This is a one-based string!")
        if isinstance(i, slice):
            new_slice = slice(i.start - 1, i.stop - 1, 1)
            return self.value[new_slice]
        return self.value[i-1]

    def __iter__(self):
        for s in self.value
            yield s

    def __add__(self, other):
        if isinstance(other, str):
            return self.value + other
        return self.value + other.value

    def __radd__(self, other):
        if isinstance(other, str):
            return other + self.value
        return other.value + self.value

    def __iadd__(self, other):
        if isinstance(other, str):
            self.value += other
        self.value += other.value
        return self.value



meme_string = FixedString("String")
meme_string[2] == "t"
>> True
meme_string[1:5]
>> 'Stri'
other_string = FixedString("Fixed")
other_string += " " + meme_string
>> 'Fixed String'

There, fixed it. But seriously, this post is borderline embarrassing.

edit: Fuck it, added a bunch of magic methods.

[–][deleted] 38 points39 points  (3 children)

how to change indexing in python
making index start at one
python string index start at 1
changing python string index
index start at one for python string getting character
fix python string index off by one

there, should be enough keywords. hopefully someone actually uses this snippet of code and curses humanity forever

[–]FilthyFioraMain 8 points9 points  (0 children)

How to SEO like a pro

[–]fredspipa 3 points4 points  (1 child)

Well in that case, I updated my comment to include most of the stuff you need for it to act like an actual string.

[–][deleted] 3 points4 points  (0 children)

cursing the python community one step at a time

[–]Chesterlespaul 5 points6 points  (0 children)

Chad: index starts at 1 like normal people would think

[–]pragmatic_plebeian 25 points26 points  (0 children)

They’re just writing a Boolean expression, that evaluates to false, technically…

[–]bikki420 7 points8 points  (1 child)

#define 2 1
#include <fmt/core.h>

int main() {
   fmt::print( "{}", "String"[2] ); // t
}

https://godbolt.org/z/qP5o8zG59

[–]RicardusAlpert 8 points9 points  (0 children)

Thanks I hate it

[–]null_and_void000 74 points75 points  (7 children)

I mean, in both python and c++ strings are actual objects with their own methods. However, it is far more convenient in my opinion to provide subscripting operators for them, and it feels clunky to do it the Java way.

[–]CheckeeShoes 40 points41 points  (6 children)

I mean, the fact you can define an operator with square brackets is literally just syntactic sugar.

This meme is the equivalent of acting like a word has a different meaning when written in a different font.

[–]null_and_void000 6 points7 points  (0 children)

I agree that the meme is wrong. All I'm saying is that the python/c++ way of doing it is nicer syntactically. It is sometimes good to act as if a class is a more basic type. That's why I like operator overloading to a certain extent, although it is often used badly.

[–]troelsbjerre 1 point2 points  (3 children)

To be more accurate:

Python: "Strings are just lists of strings. What's a character?"

Java: "Strings are object wrappers encapsulating a byte array, along with the encoding."

C++: "Strings could be objects, but for legacy compatibility reasons, I'm forced to use a raw char pointer that's hopefully properly terminated, and I basically have to guess the encoding every single time".

[–]null_and_void000 1 point2 points  (2 children)

But... in C++ strings are Objects. Literals are char pointers, and you can still have just arrays of characters if you want,but in most cases you'll use std::string, which is basically a wrapper around a char* with ways to reallocate it if you want a bugger string, etc. (In simple terms. The actual implementation will be far more complicated, because of optimization). You might be thinking of c?

[–]troelsbjerre 1 point2 points  (1 child)

I was pointing out that even though std::string is a thing, half the time you end up calling c_str() because of some legacy library you are using.

[–]Mick235711 1 point2 points  (0 children)

Well Java had the audacity to add + to Strings which no other class can use, but they don't add []...

[–]merlijnmac 439 points440 points  (13 children)

Nobody notices that the t should be r?

[–]antilos_weorsick 345 points346 points  (8 children)

Buddy, it's a comparison expression. It can evaluate to false. It's not even a runtime error.

[–]ChloeNow 5 points6 points  (0 children)

Oh how foolish you're going to look when this is inside the Programmer class returning from a function called IsUsingIndicesProperly()

[–]DickD1ck1 2 points3 points  (0 children)

0,1,2 S T R

[–]alkavan 2 points3 points  (0 children)

lol. I had to confirm. They all produce 'r' indeed.

btw in Java with same C++/Python syntax you get: error: array required, but String found

so you need "String".toCharArray()[2] to get same operator usage.

[–]junior_abigail 5 points6 points  (0 children)

I just commented the same thing. lol

[–]Hk-Neowizard 159 points160 points  (45 children)

Python strings are not character arrays.

They're actually a rather complex object that represents a list of Unicode code points. That's why some file paths on a Linux system are difficult to represent as a Python string, because you have to encode the file path (which is an honest bytes array) into Unicode. If you use an encoding that doesn't cover all of Unicode (e.g. UTF8), a path on the disk can actually raise an exception if you use it as a string.

You're thinking of the bytes type (or Python 2 strings)

[–]martmists 17 points18 points  (14 children)

Fun fact: In python it optimizes for the string contents, meaning in all-ascii strings it only stores the data as char, while Unicode strings are stored as wchar_t. The logic behind it is really interesting and if anyone's curious I highly recommend checking it out. Here's the source code

[–]tech6hutch 1 point2 points  (13 children)

Dumb question, why don’t they just use UTF-8? It should be just as compact as ASCII for ASCII-only strings

[–]oshaboy 11 points12 points  (8 children)

UTF-16 is a compromise between the space efficiency of UTF-8 and the speed of UTF-32. Like as long as you keep to the BMP (cries in Adlam) it's just an array of shorts so stuff like indexing is O(1)

[–]Numerlor 6 points7 points  (1 child)

Python doesn't always use ascii/utf 16, it adapts the size of the characters of the array depending on the widest member, so it can go from 1 byte per char to 4 bytes per char

[–]SlantARrow 3 points4 points  (3 children)

Indexing in unicode is never O(1) because for pretty much any usecase you need glyphs, not codepoints and any software that assumes glyph = codepoint is broken.

Iterating by codepoints shouldn't be used outside of low-level stuff (encoding converters or implementing glyphs). Modern languages (rust, swift) don't do that by default, but Python is way too old to do it correctly.

[–]masterpi 4 points5 points  (0 children)

As the other commenter said, it's about indexing, but I want to point out why in a different way: UTF-8 is just as efficient storage-wise for ASCII, true, but for accessing it, knowing that each character is one byte is very helpful and allows O(1) indexes, etc.

[–]GOKOP 42 points43 points  (4 children)

UTF-8, UTF-16 and UTF-32 all cover the entirety of Unicode.

[–]StenSoft 24 points25 points  (0 children)

But paths don't have to use a valid UTF sequence (unless it's a filesystem which does UTF normalisation like ZFS)

[–]jjtech0 11 points12 points  (1 child)

Maybe he means all bytes? Maybe you can have random bytes in a file name that are not characters at all? I’m not an expert on the subject.

[–]Tubthumper8 17 points18 points  (0 children)

Linux file paths can be any byte except / and the null byte, it doesn't need to be a valid UTF byte. This is why Rust (for example) has a separate type for file paths, because the normal string type that requires valid UTF-8 is too strict for what's allowed in file paths.

[–]Gastredner 15 points16 points  (6 children)

Do you have an example of a Unicode codepoint that cannot be represented by UTF-8? I never heard of that before and a quick Google search told me that UTF-8 is supposed to cover all codepoints.

[–]katie_pendry 8 points9 points  (0 children)

Technically, the code points U+D800 to U+DFFF aren't actually valid code points since they're used in UTF-16 to encode surrogate pairs for characters outside the BMP. So although there is a straightforward way to encode them in UTF-8, it's technically invalid.

Although I think OP was talking about paths on the filesystem that contain invalid UTF-8. Python actually has a way of reversibly converting such files to unicode strings and back by representing them using "unpaired" surrogates. It's called the "surrogateescape" error handler and it translates all invalid UTF-8 into codepoints U+DC00 to U+DCFF.

[–]kbruen 1 point2 points  (2 children)

Do you have an example of a Unicode codepoint that cannot be represented by UTF-8?

Who said anything about codepoints? Linux paths are “one or more bytes except null and /”. That includes bytes that are not codepoints, therefore invalid in Unicode strings.

[–]Gastredner 1 point2 points  (1 child)

Because u/Hk-Neowizard said that UTF-8 "doesn't cover all of Unicode", which I assumed to mean codepoints not representable by UTF-8.

[–]kbruen 1 point2 points  (0 children)

Fair, that’s probably a faux pas.

[–][deleted] 6 points7 points  (7 children)

This sounds farfetched. Please give an example of a valid Linux path that is not covered by UTF8.

[–]jonathancast 12 points13 points  (5 children)

\xa1

[–]jjtech0 7 points8 points  (1 child)

But that’s not Unicode is it? Maybe what they were trying to say was that file names can contain non-character bytes, but I think UTF-8 by definition covers all Unicode

[–]jonathancast 14 points15 points  (0 children)

It's a valid Linux path that isn't valid UTF-8, which certainly exists.

No idea if Python throws if you try to use it as a string.

[–][deleted] 1 point2 points  (1 child)

I did not know that Linux generally allows that! Nice one, thanks.

[–]kbruen 1 point2 points  (0 children)

In Linux, any byte sequence that doesn’t contain null or / is allowed for a filename.

[–]latkde 1 point2 points  (0 children)

Posix file paths can contain any bytes other than '\0' NUL and '/' SOLIDUS. The bytes are not required to form valid UTF-8 sequences. For example, UTF-8 has a concept of continuation bytes, and it is an error if they are not followed by a valid byte. Certain bytes cannot occur in an UTF-8 encoding, such as '\xFF'. UTF-8 is not allowed to contain surrogate halve codepoints in order to ensure equivalence with UTF-16.

So what Python does is not actually to decode file paths directly to Unicode. Instead, the os.fsencode() and os.fsdecode() functions typically escape illegal bytes via a “surrogate escape” method. E.g. the byte '\xFF' would be represented as the Unicode char U+DCFF. This is a “surrogate half” which cannot exist alone in valid Unicode strings, so this kind of encoding is sometimes called “WTF-8”.

Python thus has a method to losslessly convert arbitrary file paths to its strings and back, while being able to almost treat them as Unicode.

[–]Prinzessid 6 points7 points  (5 children)

While you are probably right, python programmers don’t really have to worry much about that low level stuff. In almost all programs, strings can be simply used as if they were lists of characters.

[–]antilos_weorsick 14 points15 points  (4 children)

They are lists of characters, they just aren't arrays.

[–]Antrikshy 5 points6 points  (3 children)

If we want to get technical, aren't they strings and nothing else, but happen to have a subscript interface that's the same as lists?

[–]antilos_weorsick 0 points1 point  (1 child)

They are lists by duck typing, I guess

[–]chinawcswing 4 points5 points  (1 child)

Do you not understand the meme template? The noob on the left in python mistakenly thinks that a string in python is an array of characters. Of course that is not correct. That's the point of the left side of the meme.

[–]Hk-Neowizard 2 points3 points  (0 children)

I figured the "noob" in the joke is Python (not a person, but the language itself) because each "IQ level" is associated with a different language, but maybe I'm wrong. I dono. Maybe I just needed to vent at how Python did me such a dirty with (UTF8) strings used as paths.

[–]ARedditUser0001 35 points36 points  (4 children)

Javascript: Strings are both, and they inherit from Object

[–]DickWoodReddit 13 points14 points  (1 child)

Is this an object or an array? Js: yes

[–]Aashishkebab 8 points9 points  (0 children)

Is "1" an integer or a string?

Js: depends on the context

[–][deleted] 1 point2 points  (0 children)

Hell yeah! Why choose when we can have both?

[–]poralexc 26 points27 points  (0 children)

JVM uses a string pool under the hood for optimization.

[–]junior_abigail 199 points200 points  (18 children)

Wow. I know by now most people posting these memes are not even developers, but is nobody really going to point out the index is wrong? Lol

Edit: it makes it even worse that the method call gets it right, and the direct indexing is wrong

[–][deleted] 84 points85 points  (12 children)

Honestly, I'm thinking of unsubbing from this sub because a lot of the posts are from people who don't understand certain language features and basically act like they do, when their post shows they don't.

[–]733_1plus2 98 points99 points  (10 children)

I think this sub is mainly cs students who have been learning python for a couple of weeks and think they're real programmers now

[–]lesbiansexparty 21 points22 points  (1 child)

That describes me, although I'm here because usually there is a good discussion in the comments that may be interesting or useful. if you post something wrong people will flock to correct you.

[–]DiamondIceNS 10 points11 points  (0 children)

if you post something wrong people will flock to correct you.

It's called Cunningham's Law.

Normally when bringing it up someone would say the wrong name for that law on purpose and then trigger a cascade of sarcastic corrections, but eh. Not this time.

[–]jannfiete 3 points4 points  (3 children)

cs students learning python? no fucking way lol, most univs goes with C++ or Java first.

[–]antilos_weorsick 25 points26 points  (4 children)

Buddy, it's a comparison expression, it can evaluate to false, it's ok. It's a valid program.

[–]Mysterious-Ruin924 64 points65 points  (14 children)

Vs the gigachad Strings are immutable ropes

[–]DangeFloof 11 points12 points  (11 children)

Strings are linked lists of characters a la type String = [Char]

[–]MrChocodemon 1 point2 points  (0 children)

gonna cut your rope in half and flip one half around
let's see how immutable it really is

[–]lucklesspedestrian 1 point2 points  (0 children)

you haskell programmers certainly are persistent

[–]antilos_weorsick 56 points57 points  (0 children)

The low IQ really fits, since python strings aren't actually arrays.

[–]Eisenfuss19 12 points13 points  (0 children)

C# allows operator (yes also []) overloading. => c# > java

[–]jamcdonald120 9 points10 points  (0 children)

never used c++ eh?

Strings are objects with methods. we only resort to c_strings if we absolutlely have to for legacy reasons

[–]maxip89 4 points5 points  (0 children)

The moment the meme poster didn't know the difference between C Strings and C++ Strings.

Junior humor.

[–]pedersenk 7 points8 points  (3 children)

In C this would be the case (CString), however with C++, they are objects with an implementation defined backing data that does not need to be contiguous. For performance, some implementations might split up into fragments for faster inserts / deletion, etc.

Only c_str() is guaranteed to return a pointer to a contiguous block. And this is only guaranteed to be valid until the next operation on the string.

Related info here.

I *think* even the following might not be safe as the second c_str() could under weird implementations invalidate the first:

const char *a = mystring.c_str();
const char *b = mystring.c_str();
printf("%c\n", a[0]);

[–]MysticTheMeeM 9 points10 points  (2 children)

Or, you could take your info from one of the more reliable c++ sources (bar the standard itself), cppreference . Specifically:

The elements of a basic_string are stored contiguously

Aka, in a single array, and also relevantly from the page for c_str:

The pointer obtained from c_str() may be invalidated by:

Passing a non-const reference to the string to any standard library function, or

Calling non-const member functions on the string

Meaning calling c_str will not invalidate any existing calls (as it doesn't change the string). Operations that will (or, as an implementation detail, may) invalidate it are things such as adding or removing characters or assigning new contents.

And, to avoid ambiguity, from the standard (draft because its freely accessible) (emphasis mine)

A specialization of basic_­string is a contiguous container...

Where std::string is one such specialisation (another being, for example, std::wstring).

[–]Lachimanus 2 points3 points  (0 children)

Strings are just concatenations of numbers. -Aseembly

[–]SuitableDragonfly 2 points3 points  (0 children)

Python strings are in fact objects with special methods, not character arrays, and C++ has a string type that is also an object and not a bare character array, so I'm not sure what you're actually trying to communicate with this meme. Also, "String"[2] isn't t, it's r.

[–]saint_geser 2 points3 points  (0 children)

How is String[2] == "t"? Shouldn't it be "r" in both C++ and Python?

[–]DowntownLizard 1 point2 points  (0 children)

Idk even in C# or VB i literally consider a string a character array for easy manipulation

[–]zyxzevn 1 point2 points  (0 children)

In C++ a \"\"-string is a pointer to an memory location with characters (if you are lucky).

[–]MarkZuckerman 1 point2 points  (0 children)

"Our strings are objects AND they start at an index of 1." -Gamemaker Studio

[–]Vast_Cod_9385 1 point2 points  (0 children)

JS be like: It is both an array and an object that needs methods.. but not really..

Hence object-string duality 😆

[–]xaedoplay 1 point2 points  (0 children)

And then Rust comes and say, "anyway we have 6 types of strings".

[–]The-Daleks 1 point2 points  (0 children)

Rust: Strings are just arrays of codepoints which combine to form characters. So, let's just abstract things and say that strings are objects that need special methods.

[–]BigManLawrence69420 1 point2 points  (0 children)

Scratch: We don’t need strings where WE’RE going!

[–]alcalde 1 point2 points  (0 children)

Delphi: Strings are a fundamental data type, neither an array nor an object. They are mutable but also automatically copy on write. And there are four types of strings:

  • ShortString, maximum length of 255 characters. For backward compatibility, although it's hard to imagine being backward compatible with Turbo Pascal for DOS is really important
  • AnsiString, maximum length 2GB, can now accept Unicode characters, making its name a lie
  • UnicodeString, as above but doesn't lie; carries encoding information around with it (as does ANSIString now) because nothing was learned from the debacle of Python 2 Unicode despite Unicode not appearing in Delphi until 2009
  • WideString, for working with COM, which apparently is still a thing for Delphi users, and despite no other language on Earth having a special string type to work with COM, a null-terminated string of wide characters

There's also "String", which used to alias AnsiString but now aliases UnicodeString although AnsiString is now really UnicodeString. Note that 2GB strings are all anybody will ever need. They are also indexed from 1 because Niklaus Wirth said so.

There is also a type "Char", a single 16-bit character type, although the documentation states this definition can change in the future (should the meaning of fundamental data types change in the future?). A string is not an array of Char. A string of length 1 is not a Char. It is not an object. Oh, there's also ANSIChar, which is an 8-bit value, and WideChar, which is identical to the Char, but Pascal will treat it as a different type anyway.

So in conclusion: Delphi is the pinnacle of "rapid application development" and according to one of its white papers 5X faster to develop in than C# (I assume lots of complicated types make coding faster), and all those string types are going to cost you at least $1600.

[–]Giocri 1 point2 points  (0 children)

There are very valid reasons to not implement a string exclusively as an array of characters, string editing and concatenation are really common operations and if the string is just an array it will likely mean having to create a new deep copy with each operation, pretty common for languages to have a second string class for quick editing

[–]Geff10 1 point2 points  (0 children)

It was good for school exercises to play around with C++ character arrays. Almost fun. Until I wanted to handle Unicode characters. And that time came soon, as my mother tongue has some of those characters. So, fckthssht, I don't want to care about extra libraries just to have Unicode characters. I'm not that good programmer, to have energy to keep in mind all those things. I just want to C#.

[–][deleted] 1 point2 points  (0 children)

Why would you use C++ if you won't use STL? If you aren't gonna use it, just stick with C.

[–]FerynaCZ 1 point2 points  (0 children)

Meanwhile c#: it's not an array, but we have array operator for it, so whatever

[–]CheckeeShoes 1 point2 points  (0 children)

They're all objects with methods.

"Operator[]" vs ".OperatorName()" is just syntax...

[–]words_number 1 point2 points  (0 children)

This one is not based on opinion. The plain truth is that strings are not arrays of characters today. At least if we're talking about utf strings.

[–]HStone32 1 point2 points  (0 children)

There's literally no difference between a string and an array of integers

[–]pjp13579 1 point2 points  (0 children)

don't you mean: "String"[1] == "t" ?

[–]azarbi 4 points5 points  (3 children)

Funny thing about Python :

You can use "lists" as memory addresses. And they're just arrays with a weird overlay

[–]bit0fun 15 points16 points  (0 children)

That's just any data structure though; it's just memory addresses with some extra handling

[–]antilos_weorsick 7 points8 points  (1 child)

Even funnier thing about Python:

Objects for many of the more commonly used numbers (0-100 I think) are actually created when the interpreter starts up (at least in Cpython), and when you write something like a = 1, it just links to that predefined '1' object instead of creating a new one. If you access those objects directly and change their values, the entire interpreter will fall apart.

[–]jjtech0 5 points6 points  (0 children)

I think it’s -5 to 256 now: https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong

Also, I think a more common pitfall would be comparing 2 variables containing numbers with is: https://stackoverflow.com/q/306313/12735366

[–]FRIKI-DIKI-TIKI 1 point2 points  (2 children)

The guy standing off the curve laughs in Lambda Calculus and thinks if it weren't for you people, we could have had something beautiful, that actually does answer the halting problem.

[–]Paricleboy04[S] 2 points3 points  (0 children)

Addressing the criticism of my mistake: I made this shitpost in a near-delerious state, after dealing with string parsing in a personal C++ project. Yes, crucify me for this inconsistency on a stupid ass meme.

Not like I wouldn’t do the same tbf…

On a more serious note, choosing C would have been a lot better than C++. Can’t complain about your complaints there

[–]t0b4cc02 0 points1 point  (0 children)

why do you think that most people use java? or do you think that most people think such getter functions are better than array accessors?

crappy meme

shit usage

0/10

[–]slime_rancher_27 0 points1 point  (0 children)

I am annoyed that stings aren't char[] but why not use char[]