简体   繁体   中英

How to use size_t as a member function in c++?

I was asked to create a function in c++. In the header file the function should look like this:

namespace w3 {
    class Text {
    public:
        size_t size() const;
    }
}

This function should return the number of record of text data from a file.

I've searched all over and I can't find a simple way of implementing a function like this. How do I implement a function like this? Its my first time using size_t and all the examples I've seen its been used IN functions and not AS functions. Thanks in advance.

It depends on what is "text record". I believe you're expected to implement a class that is able to read and store some collection of data. Once your class has a container storing your records that have been read from a file, you will be able to easily provide size() implementation, like

class MyData {
   std::vector<MyDataRecord> records;
...........
   public:
   size_t size() {return records.size();}
...........
};

size_t is just another type. On my computer for example it's exactly the same as unsigned long (but on yours it might be unsigned int for example; it's allowed to be different on different platforms).

So you can use it exactly the same way as (for example) int or char .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM