1. A string is essentially an array of characters. Think about it. Also, premature optimization for anything but terribly long strings that you won't be using.
2. Yes, and you could even access attributes via this->attr = 1234. Try to name your methods with verbs, btw, cat::number sounds a lot more like an ID variable or something.
3. Done.
1. Ok, I've thought about it. If I want the length, I'll either keep track of the number of elements added or removed, or do a linear count of the elements, depending on my ram or time budgets. Still doesnt answer the question though. It's still either constant (a lookup) or linear (a count).
2. I realise I'm being retentive about complexity, when I'm most likely talking about very small strings, but I'm trying to practise hand-optimising my code. I started working with microcontrollers recently, and it's challenged some of my assumptions about hardware. When you only have 1024 bytes of ram, every byte counts.
3. I know, I suck at examples though. Nomenclature isn't one of my strng suits as a programmer.
Name:
Anonymous2008-09-26 10:16
1. It's O(1), if you're talking about std::string.
2. Yes, but it's not really necessary and in most cases doesn't add much that can't be inferred. I'd use it only in cases where confusion might arise.
Watch out for std::list::size(), by the way. The standard specifies that one of size() and splice() is O(1), but it doesn't specify which. So, in reality, you have to treat them both as O(N), although usually it's size(). Pig disgusting.
Name:
HMA2008-09-26 10:58
>>6
Yeah, I also code for ATMegas with 1KB of RAM, but I don't use std (or strings for the matter). I can see where you're going, though. So far I've had a good time using templated classes to minimize RAM use for everything that's known at compile-time. Also don't forget to use 8-bit ints (uint8_t for AVR-libc) when you can instead of int, it's 16-bits by default so it requires twice as many cycles to process (plus the memory) - I know your snippet is probably not for an AVR, but just in case.
Yeah, i'm beginning with Arduino (arduino.cc) which is an io board with an embedded ATMega 16-8. Arduino is programmed with Wiring, which is a C++ type language with built in handling for digital and analog IO. I'm mostly being careful with what I do by trying to keep as much of everything into bytes, which as you say, are 1 cycle ops. So I don't have a problem
I've slowly come to realise that the second question I asked in OP was mainly a huge brainfart; I should have just been calling memberFunc(), without bothering with a self reference. It was for my first year comp-sci courses really. Programming for microcontrollers has just got me in a mindset where i'm conscious of my resource use.
Doing physical computing stuff is fun though. Being able to get a PWM servo driver up and running in under an hour is really satisfying.