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:
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.
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)
- STL has a lot of stuff, but I’m not sure why it’s better than normal code.
- Maps seem unnecessary unless you’re doing something weird.
- Sets are okay if you hate duplicates, I guess.
- 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!
stop using chatGPT, I guess ?
Maybe try reading some good codes which use these data structures. Start with some simpler problem where you can think what is needed for that problem and then check some good codes which have used some data structure to solve it.. If you will focus on just reading about data structures you won't understand it. Instead try thinking more about why you need to use that stl ..
"wiki said it’s for storing unique things" the other 10 isn't unique so it isn't stored! and hopefully you find its use somewhere ig
Auto comment: topic has been updated by Xbalanque (previous revision, new revision, compare).
Since you struggle with basic things you'd be better off doing some introduction to algorithms and data structures course first.
this is a very simple explanation of how maps works .
at your level just of it like this .Maps are just array but the index can be anything.
a good exemple is you can declare map<string,int> mp. mp will have a key of type string. so you can do for exemple
map<string,int>mp;
mp["first"]=20;
mp["second"] = 30;
cout<<mp["first]<<"\n";
but you can't do this with arrays .
Also wikipedia was right about find .There is this thing in c++ called iterators. you can use find on everything that has iterators like this
in the code above find returns an iterator of type pair<string,int> you can access the key using it->first and you can access the value using it->second .