Help needed in understanding basic stuff

Правка en2, от Xbalanque, 2024-12-24 15:14:14

I started learning about something called STL (Standard Template Library) in C++. Honestly, I have no idea how it works, but people say it’s important, so I tried learning it

Map : Confusing

I think it’s for storing stuff with keys and values, but honestly, it just made things more confusing. But why do we even need a map? Can’t we just use two arrays—one for keys and one for values? Seems simpler to me. Also, wikipedia said you can check if a key exists with find, but when I tried it, I got all sorts of errors.

Set : What even is this

So then I looked at set. wiki said it’s for storing unique things, but I thought arrays already do that? Anyway, here’s what I wrote:

code

The output was 10 20. Cool, I guess? But I don’t get why it ignored the duplicate. What if I want duplicates? Also, wiki said it’s sorted automatically, but it’s not like I needed it sorted here.

valarray : Why is this a thing ?

While exploring STL, I stumbled upon something called valarray. From what I read, it’s supposed to be useful for mathematical operations on arrays, but I have no idea why anyone would use it.

here is my code

The output was 2 4 6 8, which is kind of neat, I guess? But I don’t see why you’d use this instead of just writing a loop to multiply everything. Also, it has other weird operations like slicing and shifting, which sound cool but are way over my head. Does anyone actually use valarray in real problems? It feels like one of those features that’s there just to look fancy.

What I Learned (or Didn’t)

  1. STL has a lot of stuff, but I’m not sure why it’s better than normal code.
  2. Maps seem unnecessary unless you’re doing something weird.
  3. Sets are okay if you hate duplicates, I guess.
  4. valarray is confusing and maybe for mathematicians?

Help me

can someone explain why STL is such a big deal? I feel like I’m missing something obvious here. Also, if you have any tips, drop them in the comments. I’m clearly struggling, and any help would be awesome.

Thanks!

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский Xbalanque 2024-12-24 15:14:14 6 Tiny change: 'earning it.\n\n**Map ' -> 'earning it\n\n**Map '
en1 Английский Xbalanque 2024-12-24 13:16:02 2691 Initial revision (published)