小编std*_*std的帖子

为什么我们需要绑定std :: cin和std :: cout?

默认情况下,标准输入设备与标准输出设备绑定在一起: std::cin.tie (&std::cout);这样可以保证在调用输入之前刷新输出缓冲区.所以我尝试通过使用解开它们std::cin.tie(0),但似乎结果与绑定的没有区别.

#include<iostream>
using namespace std;

int main(int argc, char *argv[])
{
    char c;

    cin.tie(0)

    cout << "Please enter c:";
    cin >> c;
    cout << c ;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我测试错了吗?为什么我们需要把它们绑在一起?他们共享相同的缓冲区吗?

c++ c++-standard-library

18
推荐指数
2
解决办法
8276
查看次数

如何使用非类型参数重载<<类模板的运算符?

我正在尝试重载operator<<类模板,如下所示:

template<int V1,int V2>
class Screen
{
    template<int T1,int T2> friend ostream& operator<< (ostream &,Screen<T1,T2>&);  
    private:
        int width;  
        int length;
    public:
        Screen():width(V1),length(V2){}
};
template<int T1,int T2>
ostream& operator<< (ostream &os,Screen<T1,T2> &screen)
{
    os << screen.width << ' ' << screen.length;
    return os;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码运行正确!但我想operator<<通过不将其设置为函数模板来知道是否有任何方式重载:

friend ostream& operator<< (ostream &,Screen<T1,T2>&);

c++

6
推荐指数
2
解决办法
150
查看次数

哪个定义更好?

#include<stdio.h>
int main(int argc , char *argv[])
{
    int array[2][2] = {{1,100},{1000,10000}};

    int *pointer    = array;
    int *ppointer   = &array;
    int *pppointer  = array[0];
    int *ppppointer = &array[0];

    printf("%d\n",*pointer);
    printf("%d\n",*ppointer);
    printf("%d\n",*pppointer);
    printf("%d\n",*ppppointer);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

四个指针指向数组的第一个元素.上面显示的哪个定义更好?我不知道为什么数组和数组的值相同?

c

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

如何在迭代器上调用count并仍使用迭代器的项?

parts.count()导致所有权转移,因此parts不能再使用.

fn split(slice: &[u8], splitter: &[u8]) -> Option<Vec<u8>> {
    let mut parts = slice.split(|b| splitter.contains(b));

    let len = parts.count(); //ownership transfer

    if len >= 2 {
        Some(parts.nth(1).unwrap().to_vec())
    } else if len >= 1 {
        Some(parts.nth(0).unwrap().to_vec())
    } else {
        None
    }
}

fn main() {
    split(&[1u8, 2u8, 3u8], &[2u8]);
}
Run Code Online (Sandbox Code Playgroud)

rust

5
推荐指数
2
解决办法
3247
查看次数

为什么obj的私有值可以通过类实例改变?

#include<iostream>
using namespace std;

class A
{
    private:
        int value;
    public:
        A(int init):value(init){}
        void changevalue(A &a){a.value = 100;}//why a's value can be changed?
        void printvalue(){cout << value << endl;}
};

int main(int argc , char *argv[])
{
    A a(2);
    A b(3);

    a.changevalue(b);
    b.printvalue();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

aA类的实例,其私有值名为value,但为什么可以更改此私有值?那个参数列表是否包含在类的范围内?

c++

3
推荐指数
2
解决办法
418
查看次数

为什么open()创建的文件的权限与触摸不同?

#include<apue.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>

int main(int argc , char *argv[])
{
    int fd = open("test.txt",O_WRONLY | O_CREAT | O_TRUNC);
    int pid,status;
    const char *str_for_parent = "str_for_parent\n";
    const char *str_for_child = "str_for_child\n";
    if ((pid = fork()) < 0)
        err_sys("fork error");
    else if (pid == 0)
    {
        write(fd,str_for_child,strlen(str_for_child)+1);
        _exit(0);
    }
    wait(&status);
    write(fd,str_for_parent,strlen(str_for_parent)+1);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

test.txt是由open()创建的.但是它的permission(---------x)与我的系统中-rw-rw-r--创建的那些文件()touch或任何其他软件不同.我的umask0002

c linux

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

错误:从"底部**"到"lefta**"的无效转换

我正在尝试编译并运行这个程序.显然,它不起作用!我的问题是为什么它是从底部**到lefta**的无效转换,而底部*可以转换为lefta*?

#include<iostream>
using namespace std;
class top
{
private:
    int a;
public:
    top(int b):a(b){}
    virtual void output(){cout<<a<<endl;}
};
class lefta:virtual public top
{
private:
    int b;
public:
    lefta(int c,int d):top(c),b(d){}
    void output(){cout<<b<<endl;}
};
class righta:virtual public top
{
private:
    int c;
public:
    righta(int c,int d):top(c),c(d){}
    void output(){cout<<c<<endl;}
};
class bottom:public lefta,public righta
{
private:
    int d;
public:
    bottom(int e,int f,int g,int h):top(e),lefta(e,f),righta(e,g),d(h){}
    void output(){cout<<d<<endl;}
};
int main()
{
    bottom* bo=new bottom(1,2,3,4);
//  lefta* le=bo;
//  le->output();
    bottom** p_bo=&bo;//here
    lefta** p_le=p_bo;//here …
Run Code Online (Sandbox Code Playgroud)

c++

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

这些参数定义在python中意味着什么?

python中的许多不同的参数表示如:

urllib2.urlopen(url[, data][, timeout])

urllib2.build_opener([handler, ...])

cookielib.MozillaCookieJar(filename, delayload=None, policy=None)


urllib2.urlopen(url[, data][, timeout])
Run Code Online (Sandbox Code Playgroud)

urllib2.urlopen(url,data,timeout)
Run Code Online (Sandbox Code Playgroud)

第一个是否意味着所有url,data和timeout都可以作为列表传递?

python

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

如何在括号内grep模式?

这是字符串:

Usage:       xp (UUID: 30503c82-bf04-4f75-ab8f-129b8b350487)
Run Code Online (Sandbox Code Playgroud)

我想要这种模式

30503c82-bf04-4f75-ab8f-129b8b350487
Run Code Online (Sandbox Code Playgroud)

我可以使用grepsed来挑选它,使用如下:

grep \(.*\) -o | sed 's/[()]//g'
Run Code Online (Sandbox Code Playgroud)

我可以只用grep来完成这个操作吗?

regex grep

-1
推荐指数
1
解决办法
4483
查看次数

标签 统计

c++ ×4

c ×2

c++-standard-library ×1

grep ×1

linux ×1

python ×1

regex ×1

rust ×1