在STL Map中查找字符串键的upper_bound
我试图在 STL Map 中找到 String Key 的 upper_bound ,但它没有给我确切的结果。如果你可以运行这个程序,你会发现结果很奇怪,上限和下限都指向“qwerzzx”
我的代码中是否有任何错误或者我误解了上限操作..?
#include<iostream>
#include<cstring>
#include <map>
using namespace std;
int main()
{
map<string, int> testmap;
map<string, int>::iterator poslow;
map<string, int>::iterator posup;
testmap.insert(make_pair<string, int>("asdfghjkliopp", 1));
testmap.insert(make_pair<string, int>("asdfghjklioppswert", 1));
testmap.insert(make_pair<string, int>("sdertppswert", 1));
testmap.insert(make_pair<string, int>("sdertppswedertyuqrt", 1));
testmap.insert(make_pair<string, int>("qwerzzx", 1));
testmap.insert(make_pair<string, int>("qwerzzxasdf", 1));
testmap.insert(make_pair<string, int>("qwsdfgqwerzzx", 1));
testmap.insert(make_pair<string, int>("xcvbqwsdfgqwerzzx", 1));
testmap.insert(make_pair<string, int>("xcvbqwsdersdfgqwerzzx", 1));
poslow = testmap.lower_bound("qw");
posup = testmap.upper_bound("qw");
cout<<"Lower POS ::: "<<poslow->first<<" UPPER POS :: "<<posup->first<<"\n";
testmap.erase(poslow, posup);
}
Run Code Online (Sandbox Code Playgroud) 我想将整数的哈希集转换为逗号分隔的字符串,
所以我可以在 MySQL 查询的 where 子句中使用相同的内容。
//[MySQL - Sample table Schema]
my_table
state_id INTEGER
shop_id INTEGER
Run Code Online (Sandbox Code Playgroud)
Set<Integer> uniqueShopIds = shopService.GetShopIds(); // This returns HashSet<Integer>
String inClause = ; // **How do i convert the uniqueShopIds to comma separated String**
String selectQuery = String.format("SELECT DISTINCT(state_id) FROM my_table WHERE shop_id IN (%s);", inClause);
Run Code Online (Sandbox Code Playgroud)
如果还有其他方法,我可以直接在PreparedStatment的IN CLAUSE中使用HashSet,请分享。
我正在开发需要实现IPC机制的Mac应用程序。场景是这样的:
我的应用程序包含两个可执行文件,一个是Native Mac App(NSStatusItem app),另一个是在CPP上编码的终端应用程序。我想在这两个过程之间建立IPC通信。我希望能够从CPP向目标C发送和接收消息,反之亦然。
哪种IPC机制更适合呢?
另外,此Wiki(http://zh.wikipedia.org/wiki/Inter-process_communication#Main_IPC_methods)显示,POSIX和Windows支持IPC命名管道。我想澄清一下,如果我使用的是命名管道(我知道它是单向的),那么Mac和Objective C是否支持它?
[PS:如果可能,请提供示例代码或C ++和目标C中的IPC链接。
我执行了下面的代码,它完美地运行.因为它是指针,我只想确定.虽然我确信将字符串赋值给字符串会产生副本,即使我删除了char*,字符串var也会保留该值.
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
int main()
{
std::string testStr = "whats up ...";
int strlen = testStr.length();
char* newCharP = new char[strlen+1];
memset(newCharP,'\0',strlen+1);
memcpy(newCharP,testStr.c_str(),strlen);
std::cout << " :11111111 : " << newCharP << "\n";
std::string newStr = newCharP ;
std::cout << " 2222222 : " << newStr << "\n";
delete[] newCharP;
newCharP = NULL;
std::cout << " 3333333 : " << newStr << "\n";
}
Run Code Online (Sandbox Code Playgroud)
我只是在公司项目中更改了一些代码,而char*在C++中的函数之间传递.char*指针已复制到字符串,但char*在函数末尾被删除.我找不到任何具体原因.所以我只是删除char*,只要它被复制到一个字符串中.这会有什么问题......?
PS:我已经在Codereview中提出了这个问题,但我建议将其移至SO.所以我已将其标记为关闭并在此处发布问题.
在Linux/AIX- Unix中使用bash/ksh获取巨大CSV文件的特定列中的非数字值和行号的最快方法是什么.
假设我有以下格式的数据,
1,AAA,486254452,1F
2,BAF,265363,6A
3,AFHGJ, ,3G
...
50000,GAJGFGS,.,5H
...
100000,GHFHFFS,47,6L
Run Code Online (Sandbox Code Playgroud)
考虑到CSV中的行数可以很容易地为100,000,这是识别第3列中具有非数字值的行号的最快方法.非数字必须包含 - space,'.'.
我尝试了一个shell脚本并循环遍历文件的每一行,但这需要花费很多时间.
line=0
while read lineOfCSV
3rdCol=`echo $lineOfCSV | cut -d ',' -f3`
line=`expr $line +1`
if ! [[ $3rdCol=~ '^[0-9]+$' ]] ; then
echo "Line = :$line: NON-NUMERIC VALUE :$3rdCol:"
fi
done < data.csv
Run Code Online (Sandbox Code Playgroud)
我为上面的代码运行了80000行的CSV文件,处理时间超过10分钟.所以我停止了脚本并用谷歌搜索更快的解决方案.我找不到具体的东西.所以我在这里发布了这个问题.
我正在为My Company设计一个新的论坛,我想确认保存MySQL数据库中的论坛帖子是可扩展的,它会有很好的性能吗?帖子可能有大约400个字符(可能我将限制为400个字符).如果我在MySQL字段中保存400个字符的文本,并且表有1000万行,它会影响性能吗?我的主要约束是表现.可以请有人对此有所了解
我已经使用指针一段时间,并用于声明像指针
int *x;
Run Code Online (Sandbox Code Playgroud)
但是通过一些代码注意到,在某些情况下*似乎是在数据类型之后,比如
void* setValue(){
/* */
}
Run Code Online (Sandbox Code Playgroud)
有什么区别以及为什么这样使用它
这是我在Java中用于在Java中进行异步函数调用的一些代码:
public class AsyncLogger
{
public static asyncLog = null;
public static ExecutorService executorService = Executors.newSingleThreadExecutor();
public static AsyncLogger GetAsyncClass()
{
if(asyncLog == null)
{
asyncLog= new AsyncLogger();
}
return asyncLog;
}
public void WriteLog(String logMesg)
{
executorService.execute(new Runnable()
{
public void run()
{
WriteLogDB(logMesg);
}
});
}
public void ShutDownAsync()
{
executorService.shutdown();
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个带有静态ExecutorService的Singleton类,WriteLogDB将被称为异步函数.所以我可以异步处理WriteLogDB中的代码而不影响主流.
我可以得到这样的C++等价物吗?
c++ ×5
pointers ×2
asynchronous ×1
bash ×1
char ×1
dictionary ×1
hashset ×1
ipc ×1
java ×1
ksh ×1
mysql ×1
objective-c ×1
string ×1