Thứ Tư, 20 tháng 4, 2016

Làm thế nào để đảo chuỗi nhanh trong c++

Có 2 cách:
- Cách 1: Tự viét hàm
// A Simple C++ program to reverse a string
#include<bits/stdc++.h>
using namespace std;

// Function to reverse a string
void reverseStr(string &str)
{
    int n = str.length();

    // Swap character starting from two
    // corners
    for (int i=0; i<n/2; i++)
       swap(str[i], str[n-i-1]);
}

// Driver program
int main()
{
   string str = "geeksforgeeks";
   reverseStr(str);
   cout << str;
   return 0;
}
- Cách 2: Dùng hàm có sẵn:
// A quickly written program for reversing a string
// using reverse()
#include<bits/stdc++.h>
using namespace std;
int main()
{
   string str = "geeksforgeeks";
   
   // Reverse str[beign..end]
   reverse(str.begin(),str.end());
   
   cout << str;
   return 0;
}

Related Posts:

  • Comparator interface trong javaĐể cung cấp phương thức so sánh hai đối tượng với nhau, java cung cấp cho ta interface comparator. Interface này bắt ta phải cài đặt hàm compare trả lại 1 giá trị VD: compare(A,B) = 0 <==> A = B compare(A,B) &… Read More
  • Làm thế nào để đảo chuỗi nhanh trong c++Có 2 cách: - Cách 1: Tự viét hàm // A Simple C++ program to reverse a string #include<bits/stdc++.h> using namespace std; // Function to reverse a string void reverseStr(string &str) { int n = str.length(); … Read More
  • Interface trong classCần chú ý rằng interface khi khai báo trong class thì có thể là default, public, private, protected. Không giống khi ta khai báo 1 interface độc lập thì chỉ có thể là public // Java program to demonstrate working… Read More

1 nhận xét:

  1. According to Stanford Medical, It's in fact the SINGLE reason women in this country get to live 10 years longer and weigh 42 pounds lighter than we do.

    (And really, it is not about genetics or some secret diet and absolutely EVERYTHING to do with "how" they eat.)

    P.S, I said "HOW", not "what"...

    TAP this link to see if this little quiz can help you find out your true weight loss potential

    Trả lờiXóa

Copyright © 2025 Dịch từ nguồn geeksforgeeks.org

Distributed By My Blogger Themes | Blogger Theme By NewBloggerThemes Up ↑