其他c++代码是否存在
时间: 2023-09-24 11:09:24 浏览: 43
当然了,C++是一种非常强大的编程语言,可以用于各种各样的应用程序开发。以下是一些常见的C++代码示例:
1. 使用STL容器实现数据结构:
```c++
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main() {
// 使用vector实现动态数组
vector<int> nums = {1, 2, 3, 4, 5};
cout << "nums[3] = " << nums[3] << endl;
// 使用stack实现栈
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
cout << "st.top() = " << st.top() << endl;
st.pop();
cout << "st.top() = " << st.top() << endl;
return 0;
}
```
2. 使用多线程实现并发编程:
```c++
#include <iostream>
#include <thread>
using namespace std;
void print_msg(const string& msg, int delay) {
for (int i = 0; i < 5; i++) {
cout << msg << endl;
this_thread::sleep_for(chrono::milliseconds(delay));
}
}
int main() {
// 创建两个线程并启动
thread t1(print_msg, "Hello", 100);
thread t2(print_msg, "World", 200);
// 等待两个线程执行结束
t1.join();
t2.join();
return 0;
}
```
3. 使用OpenGL实现图形渲染:
```c++
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("OpenGL Demo");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
```
这些只是C++代码的冰山一角,C++还可以用于操作系统、游戏开发、嵌入式系统等领域。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)