{"type": "session", "harness": "opencode", "id": "cpp-stackoverflow-001", "name": "Common C++ Stack Overflow patterns"} {"type": "message", "message": {"role": "system", "content": "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."}} {"type": "message", "message": {"role": "user", "content": "Common C++ patterns from Stack Overflow"}} {"type": "message", "message": {"role": "assistant", "content": "**Common C++ Stack Overflow Patterns:**\n\n**1. How to split a string by delimiter:**\n```cpp\n#include \n#include \n\nstd::vector split(const std::string& s, char delim) {\n std::vector result;\n std::istringstream iss(s);\n std::string token;\n while (std::getline(iss, token, delim)) {\n result.push_back(token);\n }\n return result;\n}\n```\n\n**2. How to convert int to string:**\n```cpp\n// C++11+\nstd::string s = std::to_string(42);\n\n// Using ostringstream\nstd::ostringstream oss;\noss << 42;\nstd::string s = oss.str();\n```\n\n**3. How to trim a string:**\n```cpp\nstd::string trim(const std::string& s) {\n auto start = s.find_first_not_of(\" \\t\\n\\r\");\n auto end = s.find_last_not_of(\" \\t\\n\\r\");\n return (start == std::string::npos) ? \"\" : s.substr(start, end - start + 1);\n}\n```\n\n**4. How to check if string contains substring:**\n```cpp\n// C++23\nif (str.contains(\"sub\")) { }\n\n// C++11\nif (str.find(\"sub\") != std::string::npos) { }\n```\n\n**5. How to read entire file to string:**\n```cpp\nstd::ifstream f(\"file.txt\");\nstd::string content((std::istreambuf_iterator(f)),\n std::istreambuf_iterator());\n```\n\n**6. How to create a singleton:**\n```cpp\nclass Singleton {\npublic:\n static Singleton& instance() {\n static Singleton inst;\n return inst;\n }\n Singleton(const Singleton&) = delete;\n Singleton& operator=(const Singleton&) = delete;\nprivate:\n Singleton() = default;\n};\n```\n\n**7. How to implement a thread-safe queue:**\n```cpp\ntemplate\nclass ThreadSafeQueue {\n std::queue queue_;\n mutable std::mutex mutex_;\npublic:\n void push(T value) {\n std::lock_guard lock(mutex_);\n queue_.push(std::move(value));\n }\n bool pop(T& value) {\n std::lock_guard lock(mutex_);\n if (queue_.empty()) return false;\n value = std::move(queue_.front());\n queue_.pop();\n return true;\n }\n};\n```"}}