我目前有一些代码,我使用的vector是pair<string,string>.这用于存储来自XML解析的一些数据,因此,该过程在某些地方非常慢.在试图加快整个过程中,我想知道是否有将是从交换任何性能方面的优势vector<pair<string,string> >来std::map<string,string>?我可以编写代码并运行一个分析器,但我想我会看到我是否能得到一个答案,表明首先会有一些明显的性能提升.我不需要进行任何排序,我只是将项添加到向量中,然后在稍后阶段迭代内容并进行一些处理 - 我不需要排序或任何这种性质.我猜也许我不会获得任何性能提升,但我从来没有真正使用过std::map,所以我不知道没有要求或编码全部.
我正在使用platform/invoke并且我正在尝试将浮点数LPSTR和int编组到c ++函数中并且我得到以下错误:调用PInvoke函数'Game!Game.Graphics :: CreateModel'使堆栈失衡.这很可能是因为托管PInvoke签名与非托管目标签名不匹配.检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配.这是我的c#代码:
public struct Graphics
{
[DllImport(@"Graphics.dll", EntryPoint = "StartGL")]
public static extern void StartGL();
[DllImport(@"Graphics.dll", EntryPoint = "CreateModel")]
public static extern void CreateModel([MarshalAs(UnmanagedType.LPStr)]string ModelPath, [MarshalAs(UnmanagedType.LPStr)]string TexturePath,float xposh, float yposh, float zposh, float rotAngleh, float xroth, float yroth, float zroth);
[DllImport(@"Graphics.dll", EntryPoint = "rotateModel")]
public static extern void rotateModel(int id,float rotAngle,float x, float y, float z);
}
class Program
{
static void Main(string[] args)
{
OpenGL();
}
static void OpenGL()
{
Graphics.CreateModel("box.obj","asd.png",0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); …Run Code Online (Sandbox Code Playgroud) 关于如何从C++ 11将C风格的整数数组转换为新的std :: array,我想知道并且没有找到任何建议
我喜欢std :: array并且使用它很多,无论如何,如果我从二进制文件中读取数据,我会努力复制值.使用fread时有没有更简单的方法?
FILE* fp; // initalize not important here
int arr[12];
std::array<int,12> stdarr;
fread(arr, sizeof(int), 12, fp);
for(int i=0; i < 12; i++)
stdarr[i] = arr[i];
Run Code Online (Sandbox Code Playgroud) 我对以下的陈述表示怀疑;
int intvalue = 3;
int *pointInt = &intvalue;
char* p = "string";
cout << pointInt << std::endl; // this will give memory location of intvalue which is ok.
cout << p<< std::endl; // why this will give string value rather than memory location of where string is stored?
Run Code Online (Sandbox Code Playgroud) 我正在使用此模板将我更改std::array为字符串.为什么不打印出任何东西?
#include <iostream>
#include <string>
#include <array>
template<std::size_t N>
std::string to_string_2(std::array<char, N> const& arr) {
const char* str = reinterpret_cast<const char*>(arr.data());
return std::string( str, str+N );
}
int main()
{
std::array<char, 16> state = {1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,0};
std::cout << to_string_2(state) << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码编写自定义异常类:
#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <iomanip>
#include <algorithm>
class MyException : public std::exception {
public:
MyException();
explicit MyException(std::string message);
MyException(std::string source, std::string message);
MyException(int code, std::string source, std::string message);
const char *what() const throw();
private:
int exceptionCode;
std::string exceptionSource;
std::string exceptionMessage;
};
MyException::MyException() :
exceptionCode(0),
exceptionSource ("No source."),
exceptionMessage ("No message.") {}
MyException::MyException(std::string message) :
exceptionCode(0),
exceptionSource ("No source."),
exceptionMessage (std::move(message)) {}
MyException::MyException(std::string source, std::string message) :
exceptionCode(0),
exceptionSource (std::move(source)),
exceptionMessage (std::move(message)) {}
MyException::MyException(int code, …Run Code Online (Sandbox Code Playgroud) 所以我必须为我的大学课做一个简单的程序,其中包括一个fraction带分子和分母的结构,ints以及一个is_correct()检查两个条件的函数:分母!=0和<分母.如果其中两个是真的,那么我应该返回true,否则false.我有义务使用?:操作员.所以这是我的代码:
struct fraction {int n,d;
bool is_correct(){
d!=0?(return n<d?true:false):return false;
};
};
Run Code Online (Sandbox Code Playgroud)
我的意思是,我想我可以使用一个if条件d!=0,但我必须只使用?:,并g++给我:expected primary-expression before 'return'
我正在使用 Clock() 函数测试基于 ctime 库的计时器。请注意,下面的代码仅用于测试目的。
#include <ctime>
unsigned long Elapsed(void);
clock_t start = 0;
clock_t stop = 0;
int main()
{
start = std::clock();
while(1)
{
sleep(1);
cout << "Elapsed seconds: " << Elapsed() << endl;
}
return 0;
}
unsigned long Elapsed()
{
stop = std::clock();
clock_t ticks = stop - start;
double seconds = (double)ticks / CLOCKS_PER_SEC; //CLOCK_PER_SEC = 1 milion
return seconds;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,当 Elapsed() 返回计算值时,我正在执行从 double 到 unsigned long 的隐式转换。32 位系统的无符号长限制是 2,147,483,647,并且在 Elapsed() 返回 2146 后出现溢出。 …
为什么带有p1和p2的行可以合并?
他们有不同的类型
#include <functional>
#include <iostream>
void cb(int X)
{
std::cout << X << "\n";
}
int main(void) {
std::function<void(void)> p1 = std::bind(cb, 9);
std::function<void(int)> p2 = std::bind(cb, 5);
//p1 = p2;
}
Run Code Online (Sandbox Code Playgroud)
如果我取消注释行p1 = p2 - 编译错误
我正在使用https://cppinsights.io/。以下是代码(默认示例)
#include <cstdio>
int main()
{
const char arr[10]{2,4,6,8};
for(const char& c : arr)
{
printf("c=%c\n", c);
}
}
Run Code Online (Sandbox Code Playgroud)
关于cppinsights
C++ Insights 是一个基于 clang 的工具,可以进行源到源的转换。它的目标是让事情可见,这通常是有意地在幕后发生。这是关于编译器为我们做的事情的魔法。或者查看编译器的类。
这是生成的代码,这是编译器如何看待代码
#include <cstdio>
int main()
{
const char arr[10] = {2, 4, 6, 8, '\0', '\0', '\0', '\0', '\0', '\0'};
{
char const (&__range1)[10] = arr;
const char * __begin1 = __range1;
const char * __end1 = __range1 + 10L;
for(; __begin1 != __end1; ++__begin1) {
const char & c = *__begin1;
printf("c=%c\n", …Run Code Online (Sandbox Code Playgroud)