我正在尝试使用地图在cpp中编写程序...
我的目标是避免在地图中重复相同的值.
如果密钥相同,我们可以使用map来避免重复密钥.为了允许重复密钥,我们使用multimaps
如果值相同,我们如何避免?
我编写的程序允许重复值
typedef std::map<int, std::string> MyMap;
int main()
{
MyMap map;
MyMap::iterator mpIter;
int key;
string value;
int count;
for(count = 0; count < 3;count++)
{
cin >> key;
cin >> value;
std::pair<MyMap::iterator, bool> res = map.insert(std::make_pair(key,value));
}
for (mpIter=map.begin(); mpIter != map.end(); ++mpIter)
cout << " " << (*mpIter).second << endl;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用数组指针在c中编写程序...
我的目标是在标头值相同时打印错误消息.将通过命令行参数获取标头值.
如果我得到相同的标题值,即)(GET和GET)或(HEAD和HEAD),它应该打印有效,而对于所有其他情况,它应该打印无效.
我写的程序打印无效的所有组合.我不明白我在哪里弄错了.
int main(int argc,char *argv[])
{
char *str1[4] = { "GET","HEAD","POST","OPTIONS"};
char *str2[2] = {NULL};
char *string = argv[1];
const char s[2] = ",";
char *token = "";
int i = 0,j = 0,k = 0,l = 0,m = 0;
token = strtok(string, s);
while( token != NULL )
{
if(i < 2)
{
str2[i] = token;
//printf( " %s\n", token );
printf("str2[%d]= %s\n",i,str2[i]);
}
i++;
token = strtok(NULL, s);
}
for(l = 0;l < 4;l++)
printf("str1[%d] …Run Code Online (Sandbox Code Playgroud)