检索字符串的第一个字符并在C+中按字母顺序进行比较+; 我是一个新手程序员,通过教程学习C++,我想知道我怎样才能取一个字符串,取第一个字母,把它与其他2个字符串的第一个字母进行比较,然后按字母顺序排序。我不想要为我写的代码。我想了解如何做到这一点,因为其他解决方案很难理解 #include <iostream> #include <algorithm> #include <vector> #include <iterator> int main() { int size = 3; //size of your vector std::vector<std::string> vec; //initialize the vec with string type vec.reserve(size); // just to reserve the memory // input iterator takes strings from std console-size times-and put it back in to vec std::copy_n(std::istream_iterator<std::string>(std::cin), size, back_inserter(vec)); // this will do your job as per requirement std::sort(vec.begin(), vec.end()); // just to print the result std::copy(vec.begin(),vec.end(), std::ostream_iterator<std::string>(std::cout,"\n")); return 0; }

检索字符串的第一个字符并在C+中按字母顺序进行比较+; 我是一个新手程序员,通过教程学习C++,我想知道我怎样才能取一个字符串,取第一个字母,把它与其他2个字符串的第一个字母进行比较,然后按字母顺序排序。我不想要为我写的代码。我想了解如何做到这一点,因为其他解决方案很难理解 #include <iostream> #include <algorithm> #include <vector> #include <iterator> int main() { int size = 3; //size of your vector std::vector<std::string> vec; //initialize the vec with string type vec.reserve(size); // just to reserve the memory // input iterator takes strings from std console-size times-and put it back in to vec std::copy_n(std::istream_iterator<std::string>(std::cin), size, back_inserter(vec)); // this will do your job as per requirement std::sort(vec.begin(), vec.end()); // just to print the result std::copy(vec.begin(),vec.end(), std::ostream_iterator<std::string>(std::cout,"\n")); return 0; },c++,string,string-literals,C++,String,String Literals,我知道字符串只是字符数组,所以应该有一个方法来获取第一个索引 更新 这就是我的尝试: #include <iostream> #include <string> using namespace std; void valueOutput(string firstName, string secondName, string thirdName){ cout << "\n"; cout << firstName << e

我知道字符串只是字符数组,所以应该有一个方法来获取第一个索引

更新

这就是我的尝试:

#include <iostream>
#include <string>
using namespace std;

void valueOutput(string firstName, string secondName, string thirdName){
    cout << "\n";
    cout << firstName << endl;
    cout << secondName << endl;
    cout << thirdName << endl;
}

int main(){
    string name1, name2, name3;

    cout<<"Enter 3 names: "<<endl;

    cin>>name1;
    cin>>name2;
    cin>>name3;

    if(
       (name1[0] < name2[0] && name2[0] < name3[0]) 
    || (name1[0] < name3[0] && name3[0] < name2[0])
    || (name2[0] < name1[0] && name1[0] < name3[0])
    || (name2[0] < name3[0] && name3[0] < name1[0])
    || (name3[0] < name1[0] && name1[0] < name2[0]) 
    || (name3[0] < name2[0] && name2[0] < name1[0]))
    {valueOutput(name1, name2, name3);}
    else{
        return 0;
    }

}
#包括
#包括
使用名称空间std;
void valueOutput(字符串firstName、字符串secondName、字符串thirdName){
库特
我知道字符串只是字符数组,所以应该有一个
方法来获取第一个索引

看起来你指的是字符串文字

如果你有三个这样的声明

char *s1 = "Onur";
char *s2 = "Ozbek";
char *s3 = "Hello";
然后,您可以通过以下方式比较字符串文本的第一个字符

if ( s1[0] == s2[0] )

如果声明数组而不是指针,则可以使用相同的表达式

char s1[] = "Onur";
char s2[] = "Ozbek";
char s3[] = "Hello";
事实上,您甚至可以使用以下表达式

if ( "Onur"[0] == "Ozbek"[0] )

甚至像

if ( 0["Onur"] == 0["Ozbek"] )
直接比较字符串文字的第一个字符。

\include
#include <iostream>

int main() {
    std::string str1, str2, str3;
    std::cout << "Enter First String  :  ";
    getline(std::cin, str1);
    std::cout << "Enter Second String  :  ";
    getline(std::cin, str2);
    std::cout << "Enter Third String  :  ";
    getline(std::cin, str3);
    // The code is descriptive by itself till now

    // Now we gonna compare each of them.
    if(str1[0] < str2[0]) {  // Once the control flow get inside this if statement that means str1 is smaller than str2
        if (str1[0] < str3[0] && str3[0] < str2[0]) 
            std::cout << str1 << std::endl << str3 << std::endl << str2; // In here we get that str1 is smaller than str3 (and str1 is smaller than str2 as well), so the smallest is str1 and we print that. and then we compared str3 with str 2, if str3 is smaller than str2 then we will print secondly str3 and thirdly str2.
        else if (str1[0] < str3[0] && str2[0] < str3[0])
            std::cout << str1 << std::endl << str2 << std::endl << str3; // Here we get that str1 is smaller than str2 and str3 so we firstly print str1 and then we compared str2 with str3, if str2 is smaller than str3 then we will secondly print str2 and thirdly print str3.
        else
            std::cout << str3 << std::endl << str1 << std::endl << str2; // Now both the conditions mentioned above are wrong that means str3 is smaller than str1 and from the very first condition, we get that str1 is smaller than str2. That means smallest is str3 then str1 then str2 and we printed all of them in this order.
    } else { // else will be executed when str2 will be smaller than str1. So till now we get that str2 is smaller than str1. Now remember that str2 is smaller than str1.
        if (str2[0] < str3[0] && str3[0] < str1[0])
            std::cout << str2 << std::endl << str3 << std::endl << str1; // Now here str2 proved to be smaller than str3 (and we already know that str2 is smaller than str1), So str2 is the smallest and we printed firstly str2. Then we compared str3 with str1, if str3 is smaller than str1 then we will secondly print str3 and thirdly print str1.
        else if (str2[0] < str3[0] && str1[0] < str3[0])
            std::cout << str2 << std::endl << str1 << std::endl << str3;  // Now here str2 proved to be smaller than str3 (and we already know that str2 is smaller than str1), So str2 is the smallest and we printed firstly str2. Then we compared str1 with str3, if str1 is smaller than str3 then we will secondly print str1 and thirdly print str3.
        else
            std::cout << str3 << std::endl << str2 << std::endl << str1; // Now both the above conditions are false that means str3 is smaller than str2 and as we know that str2 is smaller than str1, that means str3 is the smallest, so we firstly printed str3 and then str2 and at last str1.
    }
return 0;
}
int main(){ std::字符串str1、str2、str3;
std::cout您可以使用运算符[indexNumber]获得特定位置

但是如果您想排序,应该使用
std::sort
from#include
和字符串向量
std::vector

//编辑:注意a和a对于排序或比较是不同的,搜索ascii表

我将放一个代码示例,如果您不想要解决方案,请不要进一步阅读

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
  std::vector<std::string> stringArray;
  stringArray.reserve(4);
  stringArray.push_back("Hi");
  stringArray.push_back("Hello world");
  stringArray.push_back("New c++");
  stringArray.push_back("Animal");
  std::sort(stringArray.begin(), stringArray.end());
  for(auto&a : stringArray)
  {
    std::cout << a << '\n';   
  }

}
#包括
#包括
#包括
#包括
int main()
{
std::向量字符串数组;
保留区(4);
stringArray.push_back(“Hi”);
stringArray.push_back(“Hello world”);
stringArray.push_back(“新c++”);
字符串数组。推回(“动物”);
排序(stringArray.begin(),stringArray.end());
用于(自动&a:stringArray)
{

std::cout如果您知道STL container
std::vector
,那么这项工作就容易多了。 如果你想找一些简单的东西,就在这里,希望这些评论能帮助你理解

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
int main()
{
    int size = 3;                 //size of your vector
    std::vector<std::string> vec; //initialize the vec with string type
    vec.reserve(size);            // just to reserve the memory

    // input iterator takes strings from std console-size times-and put it back in to vec
    std::copy_n(std::istream_iterator<std::string>(std::cin), size, back_inserter(vec));

    // this will do your job as per requirement
    std::sort(vec.begin(), vec.end());

    // just to print the result
    std::copy(vec.begin(),vec.end(), std::ostream_iterator<std::string>(std::cout,"\n"));
    return 0;
}
#包括
#包括
#包括
#包括
int main()
{
int size=3;//向量的大小
std::vector vec;//使用字符串类型初始化vec
vec.reserve(size);//只是为了保留内存
//输入迭代器从std控制台大小时间中提取字符串,并将其放回vec
std::copy_n(std::istream_迭代器(std::cin),size,back_inserter(vec));
//这将按照要求完成您的工作
排序(vec.begin(),vec.end());
//只是为了打印结果
std::copy(vec.begin()、vec.end()、std::ostream_迭代器(std::cout,“\n”);
返回0;
}

让我们假设字符串名为st1、st2、st3,并且您希望对它们执行上述操作

一种快速的方法是使用std::vector。将所有字符串的第一个索引值推送到该向量,然后对其进行排序。一个清晰的实现如下所示:

  vector<char> v;
  v.push_back(st1[0]);
  v.push_back(st2[0]);
  v.push_back(st3[0]);

  sort(v.begin() , v.end());
向量v; v、 推回(st1[0]); v、 推回(st2[0]); v、 推回(st3[0]); 排序(v.begin(),v.end());

您可以在这里阅读有关向量的更多信息:

找到了它。我基本上必须在每个if语句中调用函数:

#include <iostream>
#include <string>
using namespace std;

void valueOutput(string firstName, string secondName, string thirdName){
    cout << "\n";
    cout << firstName << endl;
    cout << secondName << endl;
    cout << thirdName << endl;
}

int main(){
    string name1, name2, name3;

    cout<<"Enter 3 names: "<<endl;

    cin>>name1;
    cin>>name2;
    cin>>name3;

    if(name1[0] < name2[0] && name2[0] < name3[0]){
        valueOutput(name1, name2, name3);
    }
    else if(name1[0] < name3[0] && name3[0] < name2[0]){
        valueOutput(name1, name3, name2);
    }
    else if(name2[0] < name1[0] && name1[0] < name3[0]){
        valueOutput(name2, name1, name3);
    }
    else if(name2[0] < name3[0] && name3[0] < name1[0]){
        valueOutput(name2, name3, name1);
    }
    else if(name3[0] < name1[0] && name1[0] < name2[0]){
        valueOutput(name3, name1, name2);
    } 
    else if(name3[0] < name2[0] && name2[0] < name1[0]){
        valueOutput(name3, name2, name1);
    }

}
#包括
#包括
使用名称空间std;
void valueOutput(字符串firstName、字符串secondName、字符串thirdName){

cout如果您不需要代码,我将告诉您执行相同操作的逻辑

1.散列 您可以使用哈希对字符串进行排序。其背后的思想是在第(i)个字符串的第一个字符的位置存储i。 例如: 考虑您的字符串如下::代码> AAA,BBB,CCC < /代码>

现在在位置
[用字符串[0]-“a']存储i
(这里0位置对应于'a'1位置对应于'b',…等等)

换句话说,您可以对每个字符串执行数组[str1[0]-“a']=1
,依此类推

因此,您的数组将如下所示::
array={1,2,3}

然后,您可以使用存储在哈希数组中的位置打印字符串。 我知道这看起来有点难,但我建议你看一些关于散列的教程,到时候你就会明白了

2.分类 您也可以使用排序,但随后需要存储字符串的位置。您可以使用
pair
存储字符串和字符串的位置。然后您可以使用@VaradBhatnagar的解决方案对字符串进行排序,并根据位置,按该顺序打印

如果您想知道我刚才说了什么,请参阅下面的代码以供参考。我相信您会理解这一点。(如果您不理解,请随时提出疑问)

输出 假设你的3个名字分别是name1、name2和name3,你知道字符串只是一个字符数组,所以你可以用它的Ascii数比较每个字符,不要忘记考虑大写字母和小写字母,因为它们在Ascii中的差值是32,小写字母大于大写字母,所以你可以转换为它们之间的代码。这里没有任何复杂的函数,所以很容易理解

#include <iostream>
using namespace std;

void comparing(string& name1,string& name2);
void comparing(string& name1,string& name2,string& name3);

int main(){
    string name1, name2, name3;
    cout<<"Enter 3 names: "<<endl;
    cin>>name1;
    cin>>name2;
    cin>>name3;
    comparing(name1,name2,name3);
}
void comparing(string& name1,string& name2)
{
    string t;
    for(int i=0;i<name1.size();i++){
        int x = (int)name1[i];
        int y = (int)name2[i];
        if(x<=97 && x>=122){
            x=x-32;
        }
        if(y<=97 && y>=122){
            y=y-32;
        }
        if(x>y){
            t=name1;
            name1=name2;
            name2=t;
            break;
        }}
}
void comparing(string& name1,string& name2,string& name3)
{
    comparing(name1,name2);
    comparing(name2,name3);
    comparing(name1,name3);
    comparing(name1,name2);
    cout << name1 << " " <<name2 << " " <<name3 <<endl;
}
#包括
使用名称空间std;
无效比较(字符串和名称1、字符串和名称2);
无效比较(字符串和名称1、字符串和名称2、字符串和名称3);
int main(){
字符串名称1、名称2、名称3;
库特纳2;
cin>>名称3;
比较(名称1、名称2、名称3);
}
无效比较(字符串和名称1、字符串和名称2)
{
字符串t;
对于(int i=0;iy){
t=名称1;
名称1=名称2;
name2=t;
打破
}}
}
无效比较(字符串和名称1、字符串和名称2、字符串和名称3)
{
比较(名称1、名称2);
比较(名称2、名称3);
#include <bits/stdc++.h>
using namespace std;
void hashing(string str[], int n){
    int arr[26] = {0}; // Because there are only 26 alphabets
    for(int i = 0 ; i < n ; i++){
        arr[str[i][0] - 'a'] = i + 1; // we should not store 0, as 0 defines "there was no string starting from this alphaber" 
    }
    for(int i = 0 ; i < n ; i++){
        if(arr[i] != 0){
            cout << str[arr[i] - 1] << endl;
        }
    }
}   
void sorting(string str[], int n){
    std::vector<pair <char, int> > v;
    for (int i = 0 ; i < n ; i++){
        v.push_back(make_pair(str[i][0], i));
    }
    sort(v.begin(), v.end()); // this will sort on the basis of 1st argument in our pair
    for(int i = 0 ; i < n ; i++){
        cout << str[v[i].second] << endl;
    }
}
int main(int argc, char const *argv[])
{
    int n;
    string str[30]; // it can not be larger than 26, because of the question
    cin >> n;
    for(int i = 0 ; i < n ; i++){
        cin >> str[i];
    }
    cout << "Result of hashing" << endl;
    hashing(str, n);

    cout << "Result of sorting" << endl;
    sorting(str, n);

    return 0;
}
3
cccc
bb  
aaaa 
Result of hashing
aaaa
bb
cccc
Result of sorting
aaaa
bb
cccc
#include <iostream>
using namespace std;

void comparing(string& name1,string& name2);
void comparing(string& name1,string& name2,string& name3);

int main(){
    string name1, name2, name3;
    cout<<"Enter 3 names: "<<endl;
    cin>>name1;
    cin>>name2;
    cin>>name3;
    comparing(name1,name2,name3);
}
void comparing(string& name1,string& name2)
{
    string t;
    for(int i=0;i<name1.size();i++){
        int x = (int)name1[i];
        int y = (int)name2[i];
        if(x<=97 && x>=122){
            x=x-32;
        }
        if(y<=97 && y>=122){
            y=y-32;
        }
        if(x>y){
            t=name1;
            name1=name2;
            name2=t;
            break;
        }}
}
void comparing(string& name1,string& name2,string& name3)
{
    comparing(name1,name2);
    comparing(name2,name3);
    comparing(name1,name3);
    comparing(name1,name2);
    cout << name1 << " " <<name2 << " " <<name3 <<endl;
}