1

using namespace std;在我们的代码中发现了一个通用标题,我想删除它。

因此,我需要将vector代码中出现的所有std::vector.

我需要编写一个表达式来 grep 它,忽略以下情况:

std::vector
// comment with a vector
"string with a vector"

这样我就不会像这样替换它们

std::std::vector
// comment with a std::vector
"string with a std::vector"

到目前为止,我已经设法使用表达式忽略了“std::vector”的情况

grep "^[^:]*vector" *.h

但是我在编写否定句时遇到了问题,例如:

  • 找到“ X ”而不是“ YX ”或“ Y.*X

有人可以帮忙吗?

4

4 回答 4

4

您可能会称其为作弊,但我只是将所有替换std::vectorstd::fector,将所有替换vectorstd::vector,然后将所有替换std::fectorstd::vector.

这并不酷,但对于一次性解决方案,它可能比构建复杂的表达式更快地解决您的问题。单独考虑表达式比只考虑表达式需要更长的时间。

确保您的程序不使用 std::fector :)

于 2013-09-10T11:03:32.593 回答
2

鉴于此输入

$ cat a
std::vector
// comment with a vector
"string with a vector"
replace this vector

您需要管道 grep 以便它们是 AND。否则,egrep "condition1|condition2"他们将是 OR。

       skip :vector        skip starting with "    skip starting with /
            ----                   -----                 -----
$ grep "^[^:]*vector" a | grep "^[^\"]*vector"   |     grep "^[^/]"
replace this vector
于 2013-09-10T11:02:55.427 回答
2

除非您经常使用 vector 否则您可以删除 using 声明并手动查看错误消息。通常,您只需要更改实例化向量的位置,这不应该那么频繁。

于 2013-09-10T11:54:30.357 回答
1

我通常这样做的方式是全局替换\<vector\>std::vector,然后全局替换\<std::std::std::

于 2013-09-10T11:24:42.367 回答