有时我会遇到这样的情况,我发现我需要对大型第三方文件进行#include,这样我就可以使用一个函数或一个小类,这让我感到内疚,因为我知道这已经消失,因为我的编译时间增加了当我只想要一个函数时它会编译整个文件.有没有办法绕过这个,只包括我想要的一个功能而不重写整个事情?
我和我的朋友前几天正在讨论哪种代码风格更好.
案例A:
int function()
{
largeobject a;
//do some stuff without a
//do some stuff with a
}
Run Code Online (Sandbox Code Playgroud)
案例B:
int function()
{
//do some stuff without a
largeobject a;
//do some stuff with a
}
Run Code Online (Sandbox Code Playgroud)
那么哪个代码在速度和可读性方面更好.
我似乎在交换两个向量的元素时遇到了问题.我有两个向量,x并y保存类型的对象myclass.在myclass只有一个公共的成员w.我创建指向成员的指针的矢量w的x和然后交换矢量x和y.我希望指针的向量仍然指向w成员,x但似乎并非如此.
这是一个重现我的问题的简单例子.
#include <iostream>
#include <vector>
using namespace std;
struct myclass
{
double w;
};
int main()
{
vector<myclass> x(10);
for(int i=0; i!=10; i++) x[i].w = i;
for(auto el : x) std::cout << el.w << std::endl; /* prints i */
std::cout << std::endl;
vector<double *> px(10);
for(int i=0; i!=10; i++) px[i] = &x[i].w;
for(auto el : px) std::cout …Run Code Online (Sandbox Code Playgroud) #include <cstdlib>
#include <iostream>
#include "Node.h"
#ifndef HW4_H
#define HW4_H
using namespace std;
/**
You are to implement the two functions in this class.
You can add any other method or members to it as well.
However, you cannot change their signature.
**/
class HW4{
public:
int count(Node* r) const
{
if(r->next==NULL&&r->bro==NULL) { return 0;}
if(r.isLeaf())
{
return ((1+count(r->next)+count(r->bro)));
}
count(r->next);
}
/*
This method will return true if the tree rooted at node sn can answer
the demand induced …Run Code Online (Sandbox Code Playgroud) #define BOOST_TEST_MODULE MemoryLeakTest
#include <boost/test/unit_test.hpp>
#include <iostream>
using namespace std;
BOOST_AUTO_TEST_CASE( MemoryLeakTest)
{
double* n1 = new double(100);
void* v1 = n1;
cout << sizeof(v1) << endl;
delete v1;
}
Run Code Online (Sandbox Code Playgroud)
此代码将完美地工作,没有任何错误泄漏.但是我希望能够得到对象的大小.void*我会想象有一种方法,因为delete语句知道对象v1指向的大小,以便它可以删除它所以它必须存储一些地方.
我最近看到我的一位同事做了类似的事情:
#include <iostream>
class myClass
{
public:
float X, Y;
myClass(int x, int y) : X(x), Y(y){}
};
int main()
{
char buffer[1024] = { 0 };
myClass example(12, 24);
memcpy(buffer, &example.X, sizeof(float) * 2); // Is this safe? Will X always be allocated next o Y?
}
Run Code Online (Sandbox Code Playgroud)
基本上,他正试图复制两个X和Y到char[]只需一个步骤,告诉内存拷贝读取浮动的两倍.
它绝对有效,通常情况下,我认为这很酷,继续前进.但是由于C++中所有未定义的行为.我想知道这是否能保证始终有效.Y会在X之后立即分配吗?
#include <string>
int main()
{
double randDouble = 1245.432;
std::wstring stringDouble = std::to_wstring(randDouble);
}
Run Code Online (Sandbox Code Playgroud)
当我在Visual Studio 2010中编译它时,我收到此错误
错误1错误C2668:'std :: to_wstring':对重载函数6的模糊调用
1>错误C2668:'std :: to_string':对重载函数的模糊调用
1> d:\ program files(x86)\ microsoft visual studio 10.0\vc\include\string(688):可能是'std :: string std :: to_string(long double)'
1> d:\ program files(x86)\ microsoft visual studio 10.0\vc\include\string(680):或'std :: string std :: to_string(_ULonglong)'
1> d:\ program files(x86)\ microsoft visual studio 10.0\vc\include\string(672):或
'std :: string std :: to_string(_Longlong)'
有人可以向我解释为什么编译器会混淆我做错了什么?
因此,我使用了动态堆栈,我想编写一个复制构造函数,该构造函数必须从同一类的另一个实例复制堆栈的数据。我正在尝试编写该函数,但似乎很难。有人可以帮我吗?
template<typename T=int>
class LStack
{
public:
template<typename U=int>
struct elem
{
U con;
elem<U>* link;
}
private:
elem<T>* el;
void Copystack(Lstack const& stack) // HERE
{
elem<T>* last = el;
el->con = stack->con;
while(stack->link != null)
{
var temp = new elem<T>;
temp->con = stack->con;
temp->link = stack->link;
stack = stack->link;
}
}
};
Run Code Online (Sandbox Code Playgroud) 这是一个类,它基本上是一个数字数组,我想在其上执行一些操作.
class MyClass {
public:
MyClass() { //constructor
int * array = new int[n];
for(int i = 0; i < n; i++) {
array[i] = i*i;
}
} //end constructor
int displayIthElement(int i) {
return array[i];
}
}
Run Code Online (Sandbox Code Playgroud)
我得到:错误,在displayIthElement函数中未定义标识符"array".就像数组数组在构造函数之外停止存在一样,这没有任何意义.问题是什么?
非常感谢你.
我编写了以下代码来加密文本(它远未完成):
#include <iostream>
#include <string>
using namespace std;
class Device {
string input;
string encrypted;
string decrypted;
string key;
int keysize;
public:
Device(string inp) {
input = inp;
keysize = 0;
}
int give_key(string ikey) {
key = ikey;
keysize = key.size();
}
string encrypt() {
if(key != "") {
for(int enc = 0, keyiter = 0; enc < input.size(); ++enc, ++keyiter) {
cout << "Starting loop #" << enc << endl;
if(keyiter == keysize) keyiter = 0; //Reset key …Run Code Online (Sandbox Code Playgroud) c++ ×10
arrays ×1
c++11 ×1
class ×1
coding-style ×1
declaration ×1
dynamic ×1
include ×1
invalidation ×1
performance ×1
pointers ×1
readability ×1
sizeof ×1
std ×1
swap ×1
tostring ×1