int main
{
string data;
data = "q";
char myChoice;
myChoice = data.c_str();
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用c_str(),但它不起作用。我如何转换string为char?
或者更确切地说我想问
int main()
{
char myInput;
// How to check user input is either a , b or c for char.
cout << "Your Input ";
cin >> myInput;
}
Run Code Online (Sandbox Code Playgroud)
因为string我可以使用getline(cin,mystringvariable),然后我使用 if-else 语句检查我的字符串变量。
但是炭呢?
对于C++学习的目的,我有文件class1.h,class1.cpp,class2.h和class2.cpp.我想实例化一个名为class1Objinside 的对象class2.我在哪里以及如何实例化此对象?我classObj在class2构造函数中实例化吗?
在过去,我已经创建了一个指向类的指针,该指针在那个时候运行良好,但我认为指针不是我这次应该采用的路径,因为它classObj只会在内部使用class2.
我的代码如下:
int main()
{
thread t[10];
for (int i = 0;i < 10; i++)
t[i] = thread(print,i);//thread creation
for (int i = 0;i < 10; i++)
t[i].join();//waiting maiin to threads to cpmleate execution
cout << "in main";
}
void print(int i) {
cout<<"i:"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
这是最有效的方式,还是有更有效的方法来减少运行时间?
如果我有
vector<int> vec { 1, 2, 3, 4}
Run Code Online (Sandbox Code Playgroud)
如何使用
的std ::反向
把它变成
vec {2,1,3,4}
所以我一直在关注一个教程,当我尝试编译以下代码时:
#include <glut.h>
#include <iostream>
void render(void);
void keyboard(unsigned char c, int x, int y);
void mouse(int button, int state, int x, int y);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("Test GLUT App");
glutDisplayFunc(render); // render
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop(); // initialization finished. start rendering
}
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(0.5, 0.2, 0.9);
glVertex2f(-0.5, -0.5);
glColor3f(0.1, 0.2, 0.5);
glVertex2f(0.0, -0.5);
glColor3f(0.3, 0.9, 0.7);
glVertex2f(0.0, 0.5);
glEnd();
glutSwapBuffers(); …Run Code Online (Sandbox Code Playgroud) 以下代码块之间有什么区别:
#include<iostream>
using namespace std;
int main()
{
int *p;
float *q;
cout<<p<<"\n"<<q;
}
Run Code Online (Sandbox Code Playgroud)
而这段代码:
#include<iostream>
using namespace std;
int main()
{
int *p;
float *q;
p = new int;
q = new float;
cout<<p<<"\n"<<q;
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,指针都分配了一些内存吗?
那我为什么要使用new运算符呢?
所以,我正在制作一个我希望有一个函数来生成随机数的DLL.我想知道哪些选项更有效(性能明智).
这个只是在DLL中创建一个函数,允许我得到一个随机数.
int getRand(unsigned int seed) {
int rNum; // Random Number.
srand(seed);
rNum = (rand() % // Whatever I need here.
}
Run Code Online (Sandbox Code Playgroud)
或者,只是使用srand(time(nullptr))和rand()在应用程序中性能更好?
谢谢,
约翰尼P.
我一直在阅读有效的stl.我被困在这里:
assoccontainer<int> c
for(container<int>::iterator i=c.begin();i!=c.end();++i)
if(badvalue(*i))
c.erase(i);
Run Code Online (Sandbox Code Playgroud)
给定代码有什么问题?我不明白那里的解释.
我还有其他问题:
assoccontainer意思?我找不到任何相关的文章.badvalue某种关键字吗?下面的代码返回ERROR_INSUFFICIENT_BUFFER错误:
DWORD dwReturnedDataSize;
if (!GetTokenInformation(hToken,TokenPrivileges,NULL,0,&dwReturnedDataSize))
{
CloseHandle(hToken);
return false;
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么。
我一直在阅读有关文本分类的文章,并发现了几种可用于分类的Java工具,但我仍然想知道:文本分类是否与句子分类相同!
有没有专注于句子分类的工具?
我看过一些short在C#中使用的答案,但我不确定他们是否真的回答了我的问题.是short在C++中另一个名字int?我知道你可以制作short int,这似乎能够处理很多价值但我还在开始,所以很明显,如果这short不是很多价值观.但是在这里的代码片段中:
short lives,aliensKilled;
Run Code Online (Sandbox Code Playgroud)
它不使用int short,它只是使用short.所以我想我的问题是,如果我不低于-32,768或超过32,767 ,我可以用它short作为替代品int吗?
另外,只要更换适当的东西,它是否可以替换short为int,并且它不会真正搞乱任何东西?(顺便说一下lives,aliensKilled它们都是变量名.)
当我试图包含一个名为sniffer.h的文件时,我在Eclipse中有一个"多重定义"错误.
sniffer.h:
#ifndef SNIFFER_H_
#define SNIFFER_H_
#include "sys/types.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "netdb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <queue>
#include <pthread.h>
#include <sys/timeb.h>
#include <map>
#include <algorithm>
#include <math.h>
#include <syslog.h>
#include <signal.h>
#include <asm-generic/errno-base.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
//Syslog libraries.
#include <sys/wait.h>
#include <ctime> …Run Code Online (Sandbox Code Playgroud)