在C++ 11中引入的std :: minmax函数应该返回一对分别是给定值的最低和最大值.
在这个例子中:
int a = 6, b = 5;
auto p = std::minmax(a, b);
std::cout << "p.first = " << p.first << std::endl;
std::cout << "p.second = " << p.second << std::endl;
Run Code Online (Sandbox Code Playgroud)
这与宣传和打印一样
p.first = 5
p.second = 6
现在我想有效地修改a
并b
强制b低于a,就像运行这段代码一样:
if (b > a)
std::swap(a, b);
Run Code Online (Sandbox Code Playgroud)
所以我写了这个:
int a = 5, b = 6;
std::tie(b, a) = std::minmax(a, b);
std::cout << "a = " << a << std::endl;
std::cout << "b …
Run Code Online (Sandbox Code Playgroud) 我使用以下代码创建了一个创建通知的应用程序:
// notification
Notification notification = new Notification(R.drawable.notification_icon, title, System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// parameters
String ringtone = prefs.getString(context.getString(R.string.key_notifications_ringtone), "");
if (ringtone.length() > 0) {
notification.sound = Uri.parse(ringtone);
notification.audioStreamType = AudioManager.STREAM_NOTIFICATION;
}
boolean useVibrator = prefs.getBoolean(context.getString(R.string.key_notifications_use_vibrator), false);
if (useVibrator) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
boolean useLed = prefs.getBoolean(context.getString(R.string.key_notifications_use_led), false);
if (useLed) {
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
// alert
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.notification_icon, R.drawable.icon);
contentView.setTextViewText(R.id.notification_title, title);
contentView.setTextViewText(R.id.notification_text, text);
notification.contentView = contentView;
Intent notificationIntent = new …
Run Code Online (Sandbox Code Playgroud) 我正在读一本关于Applied C++的书.
包含防护将阻止在编译源文件期间多次包含头文件.您的符号名称应该是唯一的,我们建议您根据文件名选择名称.例如,我们的文件cache.h包含这个包含保护.
#ifndef _cache_h_
#define _cache_h_
...
#endif // _cache_h_
Run Code Online (Sandbox Code Playgroud)
Lakos描述了使用冗余包含警卫来加速编译.见[Lakos96].对于大型项目,打开每个文件需要花费时间,但却发现已经定义了包含保护符号(即,文件已经包含在内).编译时间的影响可能是戏剧性的,当只使用标准的包含防护时,Lakos显示编译时间可能增加20倍.
[Lakos96]:LargeScale C++软件设计.
我没有Lakos96参考书来引用概念,所以在这里寻求帮助.
我对上述文字的疑问是
作者的意思是"对于大型项目,打开每个文件需要花费时间,但却发现已经定义了包含保护符号"?
作者的意思是"当标准包括使用警卫时"?
谢谢你的时间和帮助.
我对自己熟悉QT5并发库.我正在查看QWaitCondition示例(http://qt-project.org/doc/qt-5.0/qtcore/qwaitcondition.html#details).
这里,一个线程(线程B)读取用户输入,所有其他线程(线程A)处理此输入.
线程A:
forever {
mutex.lock();
keyPressed.wait(&mutex);
++count;
mutex.unlock();
do_something();
mutex.lock();
--count;
mutex.unlock();
}
Run Code Online (Sandbox Code Playgroud)
线程B:
forever {
getchar();
mutex.lock();
// Sleep until there are no busy worker threads
while (count > 0) {
mutex.unlock();
sleep(1);
mutex.lock();
}
keyPressed.wakeAll();
mutex.unlock();
}
Run Code Online (Sandbox Code Playgroud)
使用count变量和扩展互斥同步的原因是为了防止符号丢失.
问题是,我认为符号仍有可能丢失:想象下面的场景:
线程A处理符号,并减少反对(--count); 互斥体被释放; 然后线程A停止
线程B从睡眠状态返回,获取互斥锁,查看该计数== 0,并调用keyPressed.wakeAll(),然后解锁互斥锁.但是,wakeAll()调用无处可去,因为线程A没有等待.
我是对的,还是我错过了什么?如果我是对的,如何纠正这个例子以真正阻止它跳过符号?
看看这段代码
int main(int argc, char **argv)
{
int pid[3];
int i,tmp;
pid[0]=getpid();
if((tmp=fork()) == 0)
{
pid[2]=getpid();
printf("3. PIDY %d %d %d\n", pid[0], pid[1], pid[2]);
}
else
{
pid[2]=tmp;
if((tmp=fork()) == 0)
{
pid[1]=getpid();
printf("2. PIDY %d %d %d\n", pid[0], pid[1], pid[2]);
}
else
{
pid[1]=tmp;
printf("1. PIDY %d %d %d\n", pid[0], pid[1], pid[2]);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在输出我得到这样的smth:
1. PIDY 101 102 103
2. PIDY 101 102 103
3. PIDY 101 0 103
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我在第三个过程中得到pid [1] = 0?知道怎么解决吗?
我遇到了一个小问题和知识鸿沟
我发现有时使用If-Else样式太样板了,想用elvis-operator代替它,例如:
Dictionary<string, List<List<int>> VarsWithInfo;
Run Code Online (Sandbox Code Playgroud)
要替换:
if (VarsWithInfo.ContainsKey(ruleVar))
{
VarsWithInfo[ruleVar] = new List<List<int>> { NestingBlocks };
}
else
{
VarsWithInfo[ruleVar].Add(NestingBlocks);
}
Run Code Online (Sandbox Code Playgroud)
接着就,随即:
VarsWithInfo.ContainsKey(ruleVar) ?
VarsWithInfo[ruleVar] = new List<List<int>> { NestingBlocks } :
VarsWithInfo[ruleVar].Add(NestingBlocks);
Run Code Online (Sandbox Code Playgroud)
我知道在这种情况下与ternar运算符的连线太长,但我想知道主要原因。谢谢。
Haskell iterate
函数重复将函数应用于值以生成一系列值.例如,将(^ 2)应用于2会生成2,2 ^ 2,2 ^ 2 ^ 2,2 ^ 2 ^ 2 ^ 2,......(2,4,16,256,...)
Rust中有等价物吗?
我有一个像这样的字符串:
String orginal = "this is for test hello\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n" +
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nhello" +
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nhello";
Run Code Online (Sandbox Code Playgroud)
用户在文本区域输入内容时按"输入"很多时间,因此在java中,它会变为很多'\ r \n'.我想将其更改为:
String newStr = "this is for test hello\r\nhello\r\nhello"
Run Code Online (Sandbox Code Playgroud)
我怎么能只保留一个 \r\n
我们为x86和x64平台开发了win32应用程序.我们想在ARM平台上使用相同的应用程序.对于ARM平台,字节顺序会有所不同,即ARM平台通常使用Big endian格式.所以我们想在我们的设备应用程序中处理这个问题.
例如// In x86/x64, int nIntVal = 0x12345678
在ARM中, int nIntVal = 0x78563412
如何为ARM中的以下数据类型存储值?
请澄清一下.
此致,Raphel