tinycodinglm / cpp /cpp98-002.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
1.89 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

Explain C++98 templates and STL basics

Assistant

C++98 Templates and STL:

Template Basics:

cpp
template<typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

int x = max<int>(3, 5);    // explicit
double y = max(3.14, 2.7); // deduced

Class Templates:

cpp
template<typename T>
class Stack {
    std::vector<T> data;
public:
    void push(const T& val) { data.push_back(val); }
    T pop() { T val = data.back(); data.pop_back(); return val; }
    bool empty() const { return data.empty(); }
};

Stack<int> s;
s.push(42);

STL Containers:

  • std::vector: dynamic array
  • std::list: doubly-linked list
  • std::deque: double-ended queue
  • std::map: ordered key-value
  • std::set: ordered unique values
  • std::stack: LIFO adapter
  • std::queue: FIFO adapter

STL Algorithms:

cpp
std::sort(v.begin(), v.end());
auto it = std::find(v.begin(), v.end(), 5);
int sum = std::accumulate(v.begin(), v.end(), 0);
std::reverse(v.begin(), v.end());

Iterators:

  • InputIterator: read, single-pass
  • OutputIterator: write, single-pass
  • ForwardIterator: read/write, multi-pass
  • BidirectionalIterator: + reverse
  • RandomAccessIterator: + arithmetic