小编San*_*ahu的帖子

如何在std :: map中停止从int到float的自动转换,反之亦然

std::map在这里写了一个小程序,如下所示.

int main()
{
  map<int,float>m1;
  m1.insert(pair<int,float>(10,15.0));   //step-1
  m1.insert(pair<float,int>(12.0,13));   //step-2
  cout<<"map size="<<m1.size()<<endl;    //step -3
Run Code Online (Sandbox Code Playgroud)

我创建了一个地图,其中int类型为键,浮点类型为地图m1的值(键 - 值)对

  1. 创建一个普通的int-float对并插入到map中.

  2. 创建了一个cross float-int对并插入到map中.现在我知道隐式转换正在使这对插入映射.

在这里,我只是不希望发生隐式转换,并且应该给出编译器错误.

在我们尝试执行step-2类型操作时,我必须在此程序/映射中进行哪些更改才能使comipiler标记出错?

c++ dictionary stdmap std c++-standard-library

13
推荐指数
1
解决办法
825
查看次数

如何使stl :: map键不区分大小写

我是stl的新手.这是我的下面的程序.

typedef pair<string, int> p;
int main(int argc, char *argv[])
{
  map<string,int> st;
  st.insert(p("hello",1));   //Inserted "hello" as key to map.
  st.insert(p("HELLO",1));   //Inserted "HELLO" as key to map. 
  cout<<"size="<<st.size()<<endl;    //Output is 2 because two records found "hello" and "HELLO"
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我不想考虑重复的案例变化(大写字母为小写字母,反之亦然).这里"st.insert(p("HELLO",1));" 应该失败,因此没有.记录应为"1"而不是"2".是否有任何标志设置或类似的?

我无法找到相关问题因此发布了这个问题.

任何帮助都很感激.

c++ map

12
推荐指数
1
解决办法
8905
查看次数

写入数据时std :: ofstream中的错误处理

我有一个小程序,我初始化一个字符串并写入文件流:

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
  std::ofstream ofs(file.c_str());
  string s="Hello how are you";
  if(ofs)
     ofs<<s;
  if(!ofs)
  {
       cout<<"Writing to file failed"<<endl;
  }
  return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我的磁盘空间非常少,而且" ofs < "语句失败了.所以我知道逻辑上这是一个错误.

声明"if(!ofs)"没有遇到上述问题,因此我无法知道它失败的原因.

请告诉我,通过哪些其他选项我可以知道 "ofs <失败了.

提前致谢.

c++ ofstream

9
推荐指数
2
解决办法
2万
查看次数

子进程和父进程ID

刚刚与子进程块中的父pid值混淆.我的计划如下:

 int main(int argc, char *argv[])
  {
    pid_t pid;
    pid=fork();
    if(pid==-1){
            perror("fork failure");
            exit(EXIT_FAILURE);
    }
    else if(pid==0){
            printf("pid in child=%d and parent=%d\n",getpid(),getppid()); 
    }
    else{
            printf("pid in parent=%d and childid=%d\n",getpid(),pid);
    }
    exit(EXIT_SUCCESS);
  }
Run Code Online (Sandbox Code Playgroud)

输出:父级中的pid = 2642,子级= 2643

pid in child = 2643,parent = 1

在"高级Unix编程"中,它表示子进程可以使用getppid()函数获取父进程id.但在这里我得到"1",这是"init"进程ID.

如何在子进程块中获取父pid值..请帮助我获取输出.

我在"Linux Mint OS"中执行但在"WindRiver"操作系统中我没有遇到这个问题.该程序是否根据操作系统改变行为?

c fork pid

5
推荐指数
1
解决办法
3万
查看次数

如何在特定时间后从`std :: cin`超时读取

我写了一个小程序,

int main(int argc, char *argv[])
{
    int n;
    std::cout << "Before reading from cin" << std::endl;

    // Below reading from cin should be executed within stipulated time
    bool b=std::cin >> n;
    if (b)
          std::cout << "input is integer for n and it's correct" << std::endl;
    else
          std::cout << "Either n is not integer or no input for n" << std::endl;
    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

读取std::cin是阻塞因此程序等待直到程序或用户提供一些输入的外部中断(如信号).

我应该如何让语句std::cin >> n等待一段时间(可能使用sleep()系统调用)进行用户输入?如果用户没有提供输入并且在规定时间完成后(比如10秒),程序应该恢复到下一条指令(即if (b==1)声明之后).

c++ linux

4
推荐指数
2
解决办法
7501
查看次数

间接递归的实际应用

在一次采访中,他们让我"给出一些间接递归的实际应用".我只是回答了直接递归和间接递归之间的区别.我用谷歌搜索,但仍然没有得到任何令人满意的答案.

有关此主题的任何信息都是最受欢迎的..

c++ recursion

3
推荐指数
1
解决办法
1607
查看次数

具有前向类声明的友元类不编译

这是一个了解如何friend class在C++中使用的基本程序.

xxx有一个类yyy对象使用friend.由于类yyy 是在类xxxI 之后定义的,因此yyy使用前向声明声明了类.

#include<iostream>
using std::cout;
using std::endl;

class yyy; //Forward Declaration of class yyy

class xxx{
private:
  int a;

public:
  xxx(){a=20;yyy y2;y2.show();}       //Error//
  void show(){cout<<"a="<<a<<endl;}
  friend class yyy; //Making class yyy as freind of class xxx
};
class yyy{
private:
  int b;

public:
  yyy(){b=10;}
  void show(){cout<<"b="<<b<<endl;}
};

int main(int argc, char *argv[])
{
  xxx x1; //creating xxx object and calling constructor
  x1.show();
  return 0; …
Run Code Online (Sandbox Code Playgroud)

c++ friend friend-class

2
推荐指数
1
解决办法
799
查看次数

如何在linux中的system()函数中实现waitpid()函数

我正在经历Richard Stevens的"UNIX环境中的高级编程",我找到了这个主题.

*8.13.系统功能

*****因为系统是通过调用fork,exec和waitpid实现的,所以有三种类型的返回值.**

1.如果fork失败或者waitpid返回EINTR以外的错误,则系统返回-1并设置errno以指示错误.

2.如果exec失败,暗示shell无法执行,则返回值就好像shell已经执行了exit(127).

**3.否则,所有三个函数 - fork,exec和waitpid-success,以及来自system的返回值是shell的终止状态,格式为waitpid.******

根据我的理解,我们通过cmdstring名称fork()一个进程,exec()使它与父进程分开.

但是无法弄清楚waitpid()函数是如何调用system()函数的

创建对象时,下面的链接模糊构造函数调用没有为我提供正确的答案.

c c++ linux

2
推荐指数
1
解决办法
1972
查看次数

使用std :: cin语句进行自动超时

我写了这个程序

#include<iostream>
using namespace std;
int n;
int main(int argc, char *argv[])
{
  std::cout << "Before reading from cin" << std::endl;

   // Below reading from cin should be executed within stipulated time
   bool b=std::cin >> n;
   if (b)
      std::cout << "input is integer for n and it's correct" << std::endl;
   else
      std::cout << "Either n is not integer or no input for n" << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这里std :: cin语句将等待控制台输入并进入睡眠模式,直到我们提供一些输入并按Enter键.

我希望std :: cin语句在10秒后超时(如果用户在10秒之间没有输入任何数据,那么编译器将开始执行std :: cin语句下面的程序的下一个语句.

我能够使用多线程机制解决它.以下是我的代码:

#include<unistd.h>
#include<stdlib.h>
#include<pthread.h> …
Run Code Online (Sandbox Code Playgroud)

c++ linux multithreading

2
推荐指数
1
解决办法
1544
查看次数

pthread_exit 与 posix 线程中的返回

这是我的程序,只是为了找到 pthread_exit 和从线程返回之间的区别。

struct foo{
    int a,b,c,d;
    ~foo(){cout<<"foo destructor called"<<endl;}
};
//struct foo foo={1,2,3,4};
void printfoo(const char *s, const struct foo *fp)
{
    cout<<s;
    cout<<"struct at 0x"<<(unsigned)fp<<endl;
    cout<<"foo.a="<<fp->a<<endl;
    cout<<"foo.b="<<fp->b<<endl;
    cout<<"foo.c="<<fp->c<<endl;
    cout<<"foo.d="<<fp->d<<endl;
}
void *thr_fn1(void *arg)
{
    struct foo foo={1,2,3,4};
    printfoo("thread1:\n",&foo);
    pthread_exit((void *)&foo);
    //return((void *)&foo);
}
int main(int argc, char *argv[])
{
    int err;
    pthread_t tid1,tid2;
    struct foo *fp;
    err=pthread_create(&tid1,NULL,thr_fn1,NULL);
    if(err!=0)
            cout<<"can't create thread 1"<<endl;
    err=pthread_join(tid1,(void **)&fp);
    if(err!=0)
            cout<<"can't join with thread 1"<<endl;
    exit(0);
}
Run Code Online (Sandbox Code Playgroud)

在“*thr_fn1”线程函数中,我创建了一个对象 foo。

根据站点pthread_exit vs. return …

c++ multithreading

2
推荐指数
1
解决办法
3396
查看次数

迭代 std::array 中的记录

我正在创建一个 2D std::array,并且想要按记录(而不是按索引)迭代 2D std::array 记录

    std::array<std::array<int, 2>, 6> m_setSockarr;
    m_setSockarr = {{{0,1},{1,2},{2,3},{4,5},
                        {5,6},{6,7}}};
    for(auto i=m_setSockarr.begin();i !=m_setSockarr.end();i++)
    {
            //code here to print record wise.
    }
Run Code Online (Sandbox Code Playgroud)

我想以记录方式打印值,而不是以索引方式打印值(不使用另一个循环并最终打印 m_setScokarr[i][j])。

在 std::Map 中,我们可以使用 it->first 和 it->second 逐条打印记录(Map 是关联容器)

尝试以不同的方式打印,例如新的索引增量等,但没有进展。

我想要的输出类似于在一个 for 循环中使用 std::array 的迭代器

0,1
1,2
2,3
4,5
5,6
6,7
Run Code Online (Sandbox Code Playgroud)

想知道我们是否可以使用 std::array(SequenceContainer) 来打印记录,类似于我们使用 std::map 。

for-loop stdmap c++11 stdarray

2
推荐指数
1
解决办法
1万
查看次数

重新初始化字符串对象时发生崩溃

请求您深入了解下面的字符串类程序

class String{
private: int len;
         int size;
         char *p;
public:  
     int getlen()
     {
         return len;
     }
     int getsize()
     {
         return size;
     }
     char* getp()
     {
         return p;
     }
     String(char *p1=0)
     {
         cout<<"constructor called"<<endl;
         if(p1!=0)
         {
            len=strlen(p1);
            size=len+1;
            p=new char(sizeof(char)*(size));
            strncpy(p,p1,size);
         }
         else
         {
             len=size=0;
             p="";
         }
     }
     String(String &s1)
     {
         cout<<"copy constructor called"<<endl;
         if(&s1!=this)
         {
             cout<<"copy constructor called"<<endl;
             len=s1.getlen();
             size=len+1;
             p=new char(sizeof(char)*(size));
             //char *d=s1.getp();
             strncpy(p,s1.getp(),size);
         }
     }
     void display()
     {
         cout<<"string len="<<len<<endl;
         cout<<"string size="<<size<<endl;
         cout<<"string name="<<p<<endl;
     }
     ~String()
     { …
Run Code Online (Sandbox Code Playgroud)

c++ string

0
推荐指数
1
解决办法
105
查看次数