Visual Studio 2010 C ++代码格式化程序

时间:2011-12-10 20:00:19

标签: c++ visual-studio-2010 code-formatting

我是Visual Studio中编写C ++程序的新手。当我在VS中编写C#程序时,代码格式化程序会更改看起来像这样的代码

for(int i= 0; i<(n+  m) ;  i++){
}

进入

for(int i = 0; i < (n + m); i++)
{
}

这样更容易阅读。当我用C ++编写相同的东西时,没有任何反应。我试图选择文本并按Ctrl + E,F,但这不起作用。有没有办法在Visual Studio中改进C ++代码格式化程序?

4 个答案:

答案 0 :(得分:9)

Visual Studio无法格式化C ++ - 代码。也许有一个VS扩展。我搜索了很长时间,但从来没有找到合适的免费。

一个非常好的,免费但不易使用的代码格式化程序是GreatCode。它是一个命令行工具。

您可以将其配置为“外部工具”:

在您的高清解压缩GreatCode之后,只需转到工具 - &gt;外部工具 - &gt;添加并插入以下设置......

enter image description here

无论何时调用该工具,都会格式化实际打开的文件。

您可以在gc.cfg中根据需要配置GreatCode。我尝试了很多选项,有些很容易,有些很复杂。

如果您想要类似Microsoft的外观,只需将我的设置用作模板并自行微调:

-code_constructor_style-1
-code_split_fctdef_style-5
-code_split_decl_style-2
-overwrite_read_only-
-verbose-
-tab_out-
-space_if-
-space_return-
-space_fctcall_inparam-
-no-space_fctcall_firstparam-
-no-space_cast_after-
-space_affect_style-0
-space_autoaffect_style-0
-code_len-180
-code_keep_more_empty_lines-
-code_decl_access_to_type-
-code_decl_break_template-
-code_remove_return_paren-
-code_align_max_blanks-80
-code_class_access_eol_after-1
-code_class_access_eol_before-1
-code_split_fctcall_style-1
-code_constructor_style-1
-no-code_split_bool_before-
-no-stmt_concat_else_if-
-no-stmt_decl_remove_empty-
-no-stmt_concat_if_remove_empty-
-no-stmt_concat_else_if-
-stmt_force_brace-1
-stmt_break_dowhile-
-stmt_switch_style-1
-stmt_switch_eol-0
-stmt_class_indent-0
-stmt_static_init_style-2
-stmt_concat_inline_class-
-pp_align_to_code-
-pp_style-1
-pp_align_breakline-
-no-cmt_first_space_cpp-
-cmt_dont_modify-
-no-cmt_add_class_access-
-no-cmt_add_gc_tag-
-no-cmt_add_fct_def_class-
-no-cmt_decl_before-
-no-cmt_decl-
-no-cmt_first_line_break_first-
-no-cmt_first_line_break_last-
-no-code_split_bool_before-
-catch_eol_before-1
-no-stmt_decl_remove_empty-
-no-cmt_add_fct_def_class-
-no-cmt_add_class_access-
-no-stmt_break_alone-
-stmt_concat_inline_class-
-cmt_keep_cpp-
祝你好运!

答案 1 :(得分:8)

除了格式化工具本身之外,我使用与DirkMausF完全相同的方法。我建议你使用Artistic Style formatter:

http://astyle.sourceforge.net/

它有详细记录,并附带了许多预定义的格式样式,因此非常易于使用。

答案 2 :(得分:0)

如果您有现金支出,您可能需要查看Visual Assist。另请参阅this问题。

答案 3 :(得分:0)

很长一段时间,我在Netbeans中编写所有C ++代码,我在Visual Studio中编译它。 Netbeans正在完美地格式化代码(使用ALT + SHIFT + F)并且有许多格式化选项。