0

0

C++程序将字符串传递给函数

PHPz

PHPz

发布时间:2023-08-26 12:17:12

|

1733人浏览过

|

来源于tutorialspoint

转载

c++程序将字符串传递给函数

任何使用函数的编程语言都具有更简单、更模块化且在调试时更容易更改的代码。函数是模块化代码中非常有益的组成部分。函数可以接受参数并对其执行某些操作。与其他原始数据类型一样,我们也可以将对象类型或数组作为参数传递。在本文中,我们将看到如何在C++中将字符串类型的数据作为函数参数传递。

传递类似C++字符串的参数给函数

C++ supports stronger string objects which is actually a class with different member functions associated with them. A string object passing as an argument is similar to the passing of normal primitive datatypes. The syntax is also quite similar.

Syntax

 function_name ( string argument1, string argument2, … ) {
   // function body
}

In the following example, we will see a program to check whether a given string is a palindrome or not. There will be two functions, one will reverse the string, and another will check whether the string is palindrome or not. Let us see the algorithm and corresponding C++ implementation.

算法

  • define a function reverse(), this will take a string s
  • n := floor of (length of s / 2)
  • for i ranging from 0 to n/2; do
    • temp := s[i]
    • s[i] := s[ n - i - 1 ]
    • s[ n - i - 1 ] := temp
  • end for
  • return s
  • end of reverse() function
  • 定义一个函数 isPalindrome(),它将接受参数 s
  • revS := call reverse() by passing s to reverse the string s
  • 如果 s 和 revS 相同,则
    • return True
  • otherwise
    • return False
  • end if
  • isPalindrome()函数结束

Example

的中文翻译为:

示例

#include 
#include 

using namespace std;
string reverse( string s ) {
   char temp;
   int n = s.length();
   for( int i = 0; i < n / 2; i++ ) {
      temp = s[i];
      s[i] = s[ n - i - 1 ];
      s[ n - i - 1 ] = temp;
   }
   return s;
}

string isPalindrome( string s ) {
   string revS = reverse( s );
   if( s == revS ) {
      return "True";
   }
   else {
      return "False";
   }
}

int main()
{
   cout << "Is "racecar" a palindrome? " << isPalindrome( "racecar" ) << endl;
   cout << "Is "abcdef" a palindrome? " << isPalindrome( "abcdef" ) << endl;
   cout << "Is "madam" a palindrome? " << isPalindrome( "madam" ) << endl;
   cout << "Is "sir" a palindrome? " << isPalindrome( "sir" ) << endl;
}

Output

Is "racecar" a palindrome? True
Is "abcdef" a palindrome? False
Is "madam" a palindrome? True
Is "sir" a palindrome? False

Passing C-like character array to a function

Since C++ supports almost all that is supported by C, we can also define strings using a character array like C. To pass C-like strings to a function, it must pass a character array or a character pointer to the base address of the string. The syntaxes are like below −

立即学习C++免费学习笔记(深入)”;

Syntax

(使用字符指针)

Postme
Postme

Postme是一款强大的AI写作工具,可以帮助您快速生成高质量、原创的外贸营销文案,助您征服全球市场。

下载
 function_name ( char* , … ) {
   // function body
}

Syntax

(使用字符数组)

 function_name ( char [], … ) {
   // function body
}

Let us see the same example of palindrome checking with character array passing. Here the reverse() function will modify the array, so we must pass this string as a character array, not the character pointer. And the isPalindrome() will just check whether the string is the same as the reversed string, so it can take character pointer or character array, and the effect will be the same. The algorithm is similar so we are directly entering into the code.

Example

的中文翻译为:

示例

#include 
#include 
#include 
using namespace std;

void reverse( char s[] ) {
   char temp;
   int n = strlen( s );
   for( int i = 0; i < n / 2; i++ ) {
      temp = s[i];
      s[i] = s[ n - i - 1 ];
      s[ n - i - 1 ] = temp;
   }
}

string isPalindrome( char* s ) {
   char* sRev = (char*) malloc( strlen(s) );
   strcpy( sRev, s );
   reverse( sRev );
   if( strcmp( sRev, s ) == 0 ) {
      return "True";
   }
   else {
      return "False";
   }
}

int main()
{
   string s = "racecar";
   cout << "Is "racecar" a palindrome? " << isPalindrome( const_cast (s.c_str()) ) << endl; 
   s = "abcdef";

   cout << "Is "abcdef" a palindrome? " << isPalindrome( const_cast (s.c_str()) ) << endl; 
   s = "madam";

   cout << "Is "madam" a palindrome? " << isPalindrome( const_cast (s.c_str()) ) << endl; 
   s = "sir";

   cout << "Is "sir" a palindrome? " << isPalindrome( const_cast (s.c_str()) ) << endl;
}

Output

Is "racecar" a palindrome? True
Is "abcdef" a palindrome? False
Is "madam" a palindrome? True
Is "sir" a palindrome? False

在这个例子中,我们看到在C++中调整C样式字符串有几个步骤。对于C样式字符串,使用cstring库来获取长度、字符串比较和其他操作。从C++字符串到C字符串的转换,需要使用c_str()函数,但是这个函数返回const char*,然而我们的函数只接受char*类型的数据。对于这种情况,我们需要使用const_cast将值转换为char*类型。

Conclusion

函数可以接受原始数据类型以及数组、对象类型等。当我们使用字符串时,在C++中它们是对象类型,而在C中是字符数组类型。但是由于C++也支持C语法,所以在C++中也是有效的。传递一个字符串对象很简单,但是传递一个字符数组需要特别注意和遵循一些严格的步骤。C风格的字符串可以以数组格式或字符指针的形式传递。当我们知道函数会改变字符串本身时,我们必须将字符串作为字符数组传递,否则,从指针修改字符串是不允许的。当字符串只被使用时,我们可以使用指针或字符数组进行传递,效果是相同的。但在这种情况下,通过字符数组传递是好的,因为它会阻止对字符串的无意更新。

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.7万人学习

Django 教程
Django 教程

共28课时 | 3.4万人学习

Excel 教程
Excel 教程

共162课时 | 13.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号