小编ran*_*111的帖子

Sed在两个字符串之间提取文本

请帮我使用sed.我有一个如下文件.

START=A
  xxxxx
  xxxxx
END
START=A
  xxxxx
  xxxxx
END
START=A
  xxxxx
  xxxxx
END
START=B
  xxxxx
  xxxxx
END
START=A
  xxxxx
  xxxxx
END
START=C
  xxxxx
  xxxxx
END
START=A
  xxxxx
  xxxxx
END
START=D
  xxxxx
  xxxxx
END
Run Code Online (Sandbox Code Playgroud)

我想在START = A,END之间得到文本.我使用了以下查询.

sed '/^START=A/, / ^END/!d' input_file
Run Code Online (Sandbox Code Playgroud)

这里的问题是,我得到了

START=A
  xxxxx
  xxxxx
END
START=D
  xxxxx
  xxxxx
END
Run Code Online (Sandbox Code Playgroud)

代替

START=A
  xxxxx
  xxxxx
END
Run Code Online (Sandbox Code Playgroud)

塞德贪婪地发现.

请帮我解决这个问题.

提前致谢.

我可以使用AWK实现上述目标吗?

regex shell awk sed

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

使用const参数重载函数

函数重载可能发生在具有相同参数数量的两个成员函数之间,如果其中一个成员函数声明为const.

但是如果一个函数有一个const参数,另一个函数具有相同类型的非const参数呢?它适用于引用和指针吗?如果C++提供它,它为什么提供?如果你知道,请与我分享原因.

以下是帮助您理解上述场景的示例.

void fun(const int i)
{
    cout << "fun(const int) called ";
}
void fun(int i)
{
    cout << "fun(int ) called " ;
}
int main()
{
    const int i = 10;
    fun(i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:编译器错误: redefinition of 'void fun(int)'

void fun(char *a)
{
  cout<<"non-const fun() called";
}

void fun(const char *a)
{
  cout<<"const fun() called";
}

int main()
{
  const char *ptr = "GeeksforGeeks";
  fun(ptr);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:const fun()调用

为什么在C++中允许第二个?

c++ overloading const

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

c ++虚拟类:有趣的一点

请告诉我为什么以下程序的输出如下.我没有在c ++中获得虚拟类.请遵守以下代码:

class B
{
public:
    B(char c = 'a') : m_c(c) {}

public:
    char get_c() const { return m_c; }
    void set_c(char c) { m_c = c; }

private:
    char m_c;
};

class C: public B
{ };

class D: public B
{ };

class E
    : public C
    , public D
{ };

int main()
{
    E e;
    C &c = e;
    D &d = e;
    std::cout << c.get_c();
    d.set_c('b');
    std::cout << c.get_c() << std::endl;
    return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c++ polymorphism virtual virtual-inheritance

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

QT或MFC哪个更好学

我想有 C++ 应用程序编程的工作经验。我两难选择游戏编程还是单机编程。请建议我学习哪个框架/API 对我有益。

Qt mobile / QT gui编程,任何其他API,学习MFC而不是QT怎么样,除了qt还有其他gui框架吗?

c++ user-interface qt

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

static_cast和RTTI vs dynamic_cast

请遵守以下代码.据我所知,dynamic_cast比static_cast慢.因为它在运行时评估类型.我在这里的疑问是,如果我们将static_cast与typeid()一起使用如下,它是否需要与动态强制转换相同的时间?它会比dynamic_cast快吗?

class Shape
{ 
public:
  virtual ~Shape(){}
};
class Circle : public Shape{ };
class Square : public Shape{ };
Run Code Online (Sandbox Code Playgroud)

使用RTTI进行静态转换:

Circle c;
Shape* s = &c; // Upcast: normal and OK

// More explicit but unnecessary:
s = static_cast<Shape*>(&c);
// (Since upcasting is such a safe and common
// operation, the cast becomes cluttering)

Circle* cp = 0;
Square* sp = 0;

// Static Navigation of class hierarchies
// requires extra type information:
if(typeid(s) == typeid(cp)) // C++ RTTI …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-cast static-cast

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

C++ dynamic_cast

class A
{
};

class B:public A
{
};
int main()
{
    A a;
    B b;
    A *ap = &b;
    B *bp = dynamic_cast<B*>(ap);
    if(bp!= NULL)
       cout<<"Pass"<<endl;
    else
       cout<<"Fail"<<endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果你想进行动态演员,为什么A类应该是虚拟的呢?

c++ virtual dynamic-cast

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

写函数rand30()使用统一分布的rand100()返回1到100

请尝试编写rand30()应返回1到30的函数.

你有rand100()功能.

algorithm

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