我正在使用std::string
并需要将它们留到给定的宽度.在C++中这样做的推荐方法是什么?
样本输入:
123
Run Code Online (Sandbox Code Playgroud)
填充到10个字符.
样本输出:
123
Run Code Online (Sandbox Code Playgroud)
(123前面7个空格)
考虑一下 Makefile
% cat Makefile
main: main.o add.o
Run Code Online (Sandbox Code Playgroud)
它使用cc
而不是g++
链接目标文件
% make
g++ -Wall -pedantic -std=c++0x -c -o main.o main.cpp
g++ -Wall -pedantic -std=c++0x -c -o add.o add.cpp
cc main.o add.o -o main
main.o:main.cpp:(.text+0x40): undefined reference to `std::cout'
...
Run Code Online (Sandbox Code Playgroud)
我如何告诉(GNU)Make使用g++
(链接C++库)而不是cc
?
我想在吃豆子游戏我从一些网站上得到了使用此代码,但不得不改变UIAlertView
到UIAlertController
除了下面的代码有两个错误,我不知道如何解决(我真的很新的节目,觉得这是一个真的新手问题 - 抱歉!)
第一个错误在第4行:选择器没有已知的类方法 alertControllerWithTitle
第二个错误出现在最后一行:没有可见的接口声明选择器 show
- (void)collisionWithExit: (UIAlertController *)alert {
if (CGRectIntersectsRect(self.pacman.frame, self.exit.frame)) {
[self.motionManager stopAccelerometerUpdates];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Congratulations"
message:@"You've won the game!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil
preferredStyle:UIAlertControllerStyleAlert];
[alert show];
}
}
Run Code Online (Sandbox Code Playgroud) 我需要使用sed命令使用bash脚本从html中删除所有标记.我试过这个
sed -r 's/[\<][\/]?[a-zA-Z0-9\=\"\-\#\.\& ]+[\/]?[\>]//g' $1
Run Code Online (Sandbox Code Playgroud)
这个
sed -r 's/[\<][\/]?[.]*[\/]?[\\]?[\>]//g' $1
Run Code Online (Sandbox Code Playgroud)
但我仍然怀念一些东西,有什么建议吗?
在下面的代码中,如果由于歧义而定义了多个强制转换运算符,我希望得到编译器错误.
#include <iostream>
#include <sstream>
struct A
{
operator const char*() { return "hello world\n"; }
operator float() { return 123.0F; }
//operator int() { return 49; }
};
int main()
{
A a;
std::stringstream ss;
ss << a;
std::cout << ss.str();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
相反,只要定义了一个数字强制转换运算符,它就会编译没有错误,没有警告,并且优先使用数字强制转换operator const char *()
.声明的运算符的顺序没有区别.
然而,如果operator int()
和operator float()
都定义,那么我得到了我从一开始就预计:
'operator <<'含糊不清
是否有强制转换的优先规则,或者为什么编译器默认选择数值转换?我明白我应该明确说明我的意思,但我的问题是关于编译器的默认选择.
我是shell脚本的新手.我编写了一个shell脚本来对MySQL数据库进行增量备份.脚本采用可执行格式,手动执行时运行成功,但通过crontab执行时失败.
Crontab条目是这样的:
*/1 * * * * /home/db-backup/mysqlbackup.sh
Run Code Online (Sandbox Code Playgroud)
下面是shell脚本代码 -
#!/bin/sh
MyUSER="root" # USERNAME
MyPASS="password" # PASSWORD
MyHOST="localhost" # Hostname
Password="" #Linux Password
MYSQL="$(which mysql)"
if [ -z "$MYSQL" ]; then
echo "Error: MYSQL not found"
exit 1
fi
MYSQLADMIN="$(which mysqladmin)"
if [ -z "$MYSQLADMIN" ]; then
echo "Error: MYSQLADMIN not found"
exit 1
fi
CHOWN="$(which chown)"
if [ -z "$CHOWN" ]; then
echo "Error: CHOWN not found"
exit 1
fi
CHMOD="$(which chmod)"
if [ -z "$CHMOD" ]; then
echo …
Run Code Online (Sandbox Code Playgroud) 我可以在cpp中定义一个专门的函数,就像这样......
//标题
template<typename T>
void func(T){}
template<>
void func<int>(int);
Run Code Online (Sandbox Code Playgroud)
// cpp
template<>
void func<int>(int)
{}
Run Code Online (Sandbox Code Playgroud)
如何在cpp中的专用类中定义方法?像这样(这不起作用,我得到error C2910: 'A<int>::func' : cannot be explicitly specialized
)......
//标题
template<typename T>
struct A
{
static void func(T){}
};
template<>
struct A<int>
{
static void func(int);
};
Run Code Online (Sandbox Code Playgroud)
// cpp
template<>
void A<int>::func(int)
{}
Run Code Online (Sandbox Code Playgroud) 我尝试使用.htaccess进行301重定向.
问题:
/?view=products&id=12345 -> /8831
Run Code Online (Sandbox Code Playgroud)
旧地址和新地址之间没有关系.
由于某些原因
Redirect 301 /?view=products&id=12345 /8831
Run Code Online (Sandbox Code Playgroud)
不起作用.如果我删除问号,它没有问号.
我也尝试过:
RewriteCond %{QUERY_STRING} view=products&id=12345
RewriteRule .*$ /8831 [L,R=301]
Run Code Online (Sandbox Code Playgroud)
但它重定向我/8831?view=products&id=12345
,这对我不利.我不需要新网址中的查询字符串 -
我有简单的C代码(psuedo代码):
#define N 100000000
int *DataSrc = (int *) malloc(N);
int *DataDest = (int *) malloc(N);
memset(DataSrc, 0, N);
for (int i = 0 ; i < 4 ; i++) {
StartTimer();
memcpy(DataDest, DataSrc, N);
StopTimer();
}
printf("%d\n", DataDest[RandomInteger]);
Run Code Online (Sandbox Code Playgroud)
我的电脑:英特尔酷睿i7-3930,配备4x4GB DDR3 1600内存,运行RedHat 6.1 64位.
第一个memcpy()以1.9 GB /秒的速度发生,而接下来的三个以6.2 GB /秒的速度发生.缓冲区大小(N)太大,不能由缓存效果引起.所以,我的第一个问题:为什么第一个memcpy()这么慢?也许malloc()在你使用之前不会完全分配内存?
如果我删除了memset(),那么第一个memcpy()以大约1.5 GB /秒的速度运行,但接下来的三个以11.8 GB /秒的速度运行.几乎是加速的2倍.我的第二个问题:如果我不调用memset(),为什么memcpy()会快2倍?
我有这个ffmpeg命令
ffmpeg -ic:\ input.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb c:\ output.ts.
上面的命令成功地将input.mp4转换为output.ts.
我需要通过代码实现相同的功能(使用ffmpeg库).
有没有人知道如何在没有解码和编码的情况下从一个容器复制到另一个容器?