C++ string && C string

C++ string

main points:

  1. 无论采取什么样的字符编码,string 的一些方法处理一个 string 对象时是按照 bytes 来处理的,因此即使是采用 variable-length characters 的编码方式,string 在处理字符串时仍然只是会按照 bytes 处理,不会考虑一个 character 到底用了几个字节表示。
  2. string object 可以用 str[index] 的形式访问,每个str[index] 的类型是 char 类型。

str.substr(size_t pos = 0, size_t len = npos) const

  • 从一个string obejct 中取字串作为 string
    在这里插入图片描述

str.swap(string &x) and swap(string &x, string & y) in <algorithm>

  1. string::swap
  • 返回值 void
  • 效率没有 non-member function 中的swap 高
    string_swap
  1. <algorithm> 中的 swap
  • 返回值是 void
  • 参数是两个string
  • <algorithm>
    normal_swap

string 转换成 int => int a = stoi(str)

  • 如果string是 “04” 可以直接转换成 4
string str = "04";
	int a = stoi(str);
	cout << a;
	system("pause");
	return 0;

result

document:
string

C string

main points:

  1. C-string is a character array the last element of which is the terminating zero.
    c_string

strcpy and strcpy_s

  1. strcpy
  • 有返回值,返回的是 字符串赋值的目标指针值
    strcpy
  1. strcpy_s
  • strcpy 是不安全的,因为不能保证 destination 能完全 handle source 中的所有字符,有可能造成 overflow
  • strcpy_s 在strcpy 的基础上添加了一个参数指明向 destination 中赋值的元素的个数,可以避免 overflow,但是可能会导致原来string object 的值被截断,不完整
  • 所以只有在确保 destination 可以handle source 中的所有字符时再用这个函数
  • 考虑到兼容性,用strcpy 会更好一些,此时需要在 VS 中设置 _CRT_SECURE_NO_WARNINGS

strcpy_s

cstring (string.h)
cstring

tansform between C string and C++ string

string => char*:

  1. string.c_str ()
  • 返回一个指向 char 数组的指针,具体的是 const char* pointer = str.c_str(), 因此不可以将 str.c_str() 的返回值赋值给 char * pointer
  • 数组中包含 string object 中的所有字符,并且在最后添加一个结束符 ‘\0’,因此数组中元素的个数为 str.length() + 1
  • 如果始终使用const char* pointer 来访问对应string 的每个character, 可以通过 *(pointer + n)访问所有字符,并且会和string object 的更新同同步
  • 如果用 str.c_str() 一次把当前的 string 值存入了一个新开辟的数组中,那么随着string obejct 的更新,数组元素和string object 的值可能不同步

例1:

string str("elsa song hello");
	// 两种处理 str.c_str() 结果的方式
	char * cstr = new char[str.length() + 1];
	strcpy_s(cstr,str.length()+1, str.c_str());
	const char * arr = str.c_str();

	for (int i = 0; i < str.length() + 1; i++)
		cout << cstr[i];
	cout << endl;
	for (int i = 0; i < str.length() + 1; i++)
		cout << *(arr + i);
	cout << endl;

	// 更改 str 的指定元素值
	str[0] = 'w';

	cout << str << endl;

	for (int i = 0; i < str.length() + 1; i++)
		cout << cstr[i];
	cout << endl;
	for (int i = 0; i < str.length() + 1; i++)
		cout << *(arr + i);
	cout << endl;

result

例2:
example_2

document:
c_str

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 FORTRAN90 中调用 C++ 函数,需要使用 `iso_c_binding` 模块来声明 C++ 函数,并使用 `c_f_pointer` 函数将 C++ 函数指针转换为 FORTRAN90 中的指针类型。下面是一个示例代码,展示了如何声明并调用 C++ 中的函数: ``` #include <string> #include <iostream> extern "C" { void my_cpp_function(const std::string& str) { std::cout << "The string received in C++ is: " << str << std::endl; } } ``` 在 FORTRAN90 中,您需要使用 `iso_c_binding` 模块来声明 C++ 函数,并使用 `c_f_pointer` 函数将函数指针转换为 FORTRAN90 中的指针类型。示例代码如下: ``` program main use iso_c_binding interface subroutine my_cpp_function(str) bind(C, name="my_cpp_function") import :: C_CHAR type(c_ptr), value :: str end subroutine my_cpp_function end interface character(len=20) :: my_string = "Hello from FORTRAN" type(c_ptr) :: my_string_ptr ! Convert the FORTRAN string to a C string pointer call c_f_pointer(c_loc(my_string), my_string_ptr) ! Call the C++ function using the C string pointer call my_cpp_function(c_char_pointer(my_string_ptr)) end program main ``` 在代码中,首先使用 `interface` 关键字声明 C++ 函数 `my_cpp_function`,并使用 `bind(C)` 来指定函数的 C 风格命名。使用 `C_CHAR` 来声明 C++ 函数的参数类型。`c_f_pointer` 函数将字符串类型的 `my_string` 转换为指针类型,并将其传递给 `my_cpp_function` 函数。在调用 `my_cpp_function` 函数时,需要使用 `c_char_pointer` 函数将字符串指针转换为 C 字符串指针。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值