为什么打印下面的代码1,即使我在地图中插入了两个元素?
#include <iostream>
#include <map>
#include <string>
#include <utility>
struct Foo
{
Foo(int bar, const std::string& baz)
: bar(bar)
, baz(baz)
{}
int bar;
std::string baz;
bool operator<(const Foo& rhs) const
{
if (bar < rhs.bar && baz < rhs.baz)
{
return true;
}
else
{
return false;
}
}
};
int main()
{
Foo first(0, "test");
Foo second(1, "test");
std::map<Foo, std::string> m;
m.insert(std::make_pair(first, "test"));
m.insert(std::make_pair(second, "test1"));
std::cout << m.size() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
第二个电话insert()说我们已经在地图中有该项目.为什么?
由于输入错误,我之前的问题 …
我正在试图弄清楚makefile并且已经有一个我想测试它的小项目目录.可悲的是,我似乎无法满足编译器的需求.g ++警告我,我需要-std=c++11我拥有的标志,并进一步将错误打印到C++ 11相关功能(如线程).我不知道我做错了什么.
以下是第一行错误:
make
g++ -c -o Graphics.o Graphics.cpp
In file included from /usr/include/c++/5/thread:35:0,
from Graphics.h:4,
from Graphics.cpp:1:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
^
In file included from Graphics.cpp:1:0:
Run Code Online (Sandbox Code Playgroud)
在这里Makefile:
csrc=$(wildcard *.c)
cppsrc=$(wildcard *.cpp)
obj=$(cppsrc:.cpp=.o) $(csrc:.c=.o)
CXXC = g++
#CXXCFLAGS = -std=c++14
EXEC = myprog …Run Code Online (Sandbox Code Playgroud) 我有一个压缩的 tar 存档,我想从存档深处提取一个文件到当前工作目录。
有没有比 更好tar的方法,或者没有目录组件的方法来提取它?
我现在正在做这样的事情:
tar xfvz $file -C $destination $folder"/"$file
cd $destination"/"$folder
mv $file ../$file
rm -r $folder
Run Code Online (Sandbox Code Playgroud)
但是我有时候删除错了$folder。
例如,我的存档是:mytar.tar.gz。在里面我有myfolder/mysecondfolder/hello.txt。
我想提取myfolder/mysecondfolder/hello.txt作为hello.txt当前目录。
我想知道来自Accelerated C++ Section 14.1.2的代码是否会在fn调用时导致内存泄漏:
class IntPtr {
public:
IntPtr (): p(NULL) {};
IntPtr (int *ip): p(ip) {};
IntPtr (const IntPtr& other) { p = new int(*other.p); };
IntPtr& operator= (const IntPtr&);
~IntPtr () { delete p; };
private:
int *p;
}
IntPtr& IntPtr::operator= (const IntPtr& other) {
if (&other != this) { delete p; p = new int(*other.p); }
return *this;
}
void fn () {
IntPtr a;
a = new int(9);
}
Run Code Online (Sandbox Code Playgroud)
这是我认为a = new …
在 Linux(Ubuntu 平台)设备上,我使用一个文件来保存关键任务数据。
有时(大约 10,000 个案例中发生一次),文件会因不明原因而损坏。特别是,文件被截断(而不是一些 kbyte,它只有大约 100 个字节)。
现在,按照软件的顺序
紧接着,文件可能会再次打开 (4),并且正在执行其他操作。
到目前为止,我还没有注意到fflush(被调用fclose)不会写入文件系统,而只会写入中间缓冲区。可能是因为 3) 和 4) 之间的时间太短,并且 2) 的更改尚未写入磁盘,所以当我用 4) 重新打开时,我得到一个截断的文件,当它再次关闭时会导致永久丢失那些数据?
fsync()在这种情况下我应该在每次文件写入后使用吗?
停电需要考虑什么?数据损坏不太可能与断电有关。
在C中使用时,我不明白"ab"和"rb+"模式之间的区别fopen().
为什么我会选择一个而不是另一个?
我已经研究了所有可能的方法,但是我很难消化malloc ie malloc(sizeof(10))
和calloc都calloc(2,sizeof(5))分配相同的连续内存这一事实,忽略了calloc初始化为零并且比malloc工作相对慢的其他事实.所以这就是我的想法.
我认为在32位系统上,如果我们调用malloc并说malloc(sizeof(10))当时malloc将进入堆并分配12个字节的内存,因为对于32位系统,内存包以4个字节为一组排列,以便分配10个在最后一个块中填充2个字节需要3个字节.
类似地,如果我们调用calloc并说calloc(2,sizeof(5))它将分配2个块,每个块大小为8个字节,总共16个字节,因为由于相同的原因,内存在4个字节的包中,并且分配5个字节,两个4个字节的块是在一个块中使用,将提供3个字节的填充.
所以这就是我对malloc和calloc的看法.我可能是对或错但请告诉我.
c memory pointers memory-management dynamic-memory-allocation
考虑这个将窄字符串转换为宽字符串的函数:
std::wstring convert(const std::string& input)
{
try
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
return converter.from_bytes(input);
}
catch(std::range_error& e)
{
std::size_t length = input.length();
std::wstring result;
result.reserve(length);
for(std::size_t i = 0; i < length; i++)
{
result.push_back(input[i] & 0xFF);
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
我很难理解在后备路径中需要这个表达式:
result.push_back(input[i] & 0xFF);
Run Code Online (Sandbox Code Playgroud)
为什么字符串中的每个字符都被 0xFF (0b11111111) 屏蔽?
我使用 C++ 已经很长时间了,但我来自 Windows。
我接到了一个使用 CMake 的项目。我用谷歌搜索试图学习它。
我运行cmake .
我假设它总是寻找CMakeLists.txt并生成makefile。
这会创建更多的 cmake 文件和 make 文件。然后我被指示跑make package。我假设package只是一个可以是任何东西的目标名称。
然后我收到错误:
c++: error: unrecognized command line option ‘-mthumb-interwork’
c++: error: unrecognized command line option ‘-mfloat-abi=hard’
c++: error: unrecognized command line option ‘-mfpu=neon’
Run Code Online (Sandbox Code Playgroud)
我认为这是因为我正在尝试针对与我所使用的架构不同的架构进行编译。我假设所有的 cmake 文件都会为我正确设置。我还假设没有什么是专有的。
我在 CMakeLists.txt 中看到了这一行
SET(CMAKE_CXX_FLAGS "-march=armv7-a -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a9 -std=c++11 -O3")
Run Code Online (Sandbox Code Playgroud)
如果我查看官方 cmake 文档,它会说,并且我引用了 “所有构建类型的标志”。....不是很有帮助
我认为它没有使用正确的编译器,但我也看不到 CMakeLists.txt 中编译器的指定位置。
问题是:为什么这些标志无法识别?
我有一个问题,我已经尽可能地缩小了范围。我想分/etc/passwd两次计算行数。一次作为参考,一次检测任何变化。
我初始化一个passwd结构并计算其中的条目数。然后我打电话setpwent()。完成后,我初始化了第二个passwd结构,请注意,我添加了一个sleep调用,因此有足够的时间添加另一个用户。问题是新结构与第一个相同,即使我添加了一个新用户并在添加后初始化了一个新结构。所以没有区别。
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
int main()
{
for(;;)
{
struct passwd *i;
int y = 0;
while((i = getpwent()) != NULL)
y++;
printf("Lines : %d\n", y);
setpwent();
sleep(30);
struct passwd *j;
int x = 0;
while((j = getpwent()) != NULL)
x++;
printf("Lines : %d\n", x);
setpwent();
}
}
Run Code Online (Sandbox Code Playgroud)