Java to c++

Sorry I know Java more than c++ and I did this challenge given to me in Java but its suppose to be in c++.
If there is anyone that is experienced in both Java and c++.
Please help me change this code to c++




import java.util.*;
import java.util.Set;
import java.util.HashSet;

public class username {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String input = scanner.nextLine();
int numberOfNames = (int)Double.parseDouble(input);

String arr[] = new String[numberOfNames];
Set <String> unique = new HashSet<>();

//add the names to the major array for the database
for (int i =0; i<numberOfNames; i++) {
String names = scanner.next();
arr[i] = names;
}

//unique
for (int j = 0; j< arr.length;j++) {
if (!unique.contains(arr[j])) {
unique.add(arr[j]);
}
}


for (int i = 0; i<arr.length; i++) {
int counter= 1;
for (int j=i+1; j<arr.length; j++) {
String no = (arr[i]);
String no2 = (arr[j]);
if (no.equals(no2)) {
counter +=1;
String counterString = Integer.toString(counter);
arr[j] += counterString;
}
}
}

int count = 0;
for (String data : unique) {
count +=1;
}
System.out.println(count);
for (String namesg:arr){

System.out.println(namesg);
}


}

}
You must format your code. Some forums reject a post with unformatted code.

This is the Java stuff.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.*;
import java.util.Set;
import java.util.HashSet;

public class username {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String input = scanner.nextLine();
        int numberOfNames = (int)Double.parseDouble(input);

        String arr[] = new String[numberOfNames];
        Set <String> unique = new HashSet<>();

        //add the names to the major array for the database
        for (int i =0; i<numberOfNames; i++) {
            String names = scanner.next();
            arr[i] = names;
        }

        //unique
        for (int j = 0; j< arr.length;j++) {
            if (!unique.contains(arr[j])) {
                unique.add(arr[j]);
            }
        }

        for (int i = 0; i<arr.length; i++) {
            int counter= 1;
            for (int j=i+1; j<arr.length; j++) {
                String no = (arr[i]);
                String no2 = (arr[j]);
                if (no.equals(no2)) {
                    counter +=1;
                    String counterString = Integer.toString(counter);
                    arr[j] += counterString;
                }
            }
        }

        int count = 0;
        for (String data : unique) {
            count +=1;
        }
        System.out.println(count);
        for (String namesg:arr){
            System.out.println(namesg);
        }
    }
}


This is the C++ stuff.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include <set>
#include <vector>
#include <functional>
#include <iostream>

int main() {
    std::string line;
    std::getline(std::cin, line);
    auto numberOfNames = atoi(line.c_str());

    std::vector<std::string> names; // your code calls this arr
    names.reserve(numberOfNames);
    for (auto i = 0; i != numberOfNames && std::getline(std::cin, line); ++i)
        names.emplace_back(std::move(line));

    std::set<std::size_t> unique;
    for (const auto& name : names)
        unique.insert(std::hash<std::string>{}(name));

    for (auto i = 0; i < numberOfNames; ++i) {
        std::size_t counter{1};
        for (auto j{i + 1}; j < numberOfNames; ++j) {
            const auto& str_i = arr[i];
            const auto& str_j = arr[j];
            if (str_i == str_j)
                arr[j] += std::to_string(++counter);
        }
    }

    // you can do the printing yourself
}
Last edited on
I don't I I'm right but is this correct or
1
2
3
4
5
6
   
      int count = 0;
        for (int i=0; i < unique; I++) {
            count ++;
        }
        Cout << count;


Though here I can't
1
2
        for (String namesg:arr){
            System.out.println(namesg);
Last edited on
Please anyone
The first part is just unique.size().

Surely you must be able to print the values in a container.
Surely I can't bro if I would I couldn't ask.please
Oooh thanks I did it
1
2
for ( auto w: names)
       Cout << w;
almost!
in c++ case matters. Cout is not correct. its cout.

The output comes like
Alice
Alice
2
Bob
John
Bob
2
Alice
3

I want it to be like anyone who can help me
Alice
Alice2
Bob
John
Bob2
Alice3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <string>
#include <set>
#include <vector>
#include <functional>
#include <iostream>
using namespace std;
int main() {
    string line;
    getline(cin, line);
    auto numberOfNames = atoi(line.c_str());

    vector<string> names; 
    names.reserve(numberOfNames);
    for (auto i = 0; i != numberOfNames && getline(cin, line); ++i)
        names.emplace_back(move(line));

    set<size_t> unique;
    for (const auto& name : names)
        unique.insert(hash<string>{}(name));
    for (auto i = 0; i < numberOfNames; ++i) {
        size_t counter{1};
        for (auto j{i + 1}; j < numberOfNames; ++j)
         {
            const auto& str_i = names[i];
            const auto& str_j = names[j];
            if (str_i == str_j)
                names[j] +=to_string(++counter);
               
        }
    }   
        cout << unique.size();
    for(auto w:names)
    { cout <<"\n";
      cout << w;
    }
}


Not sure why you've included using namespace std, and ignored the indentation and blank line convention.
Last edited on
it works the way you said you wanted it to work for me when I run it on my machine.

you occasionally see this kind of thing if you somehow scramble dos and unix end of lines. both dos eol characters make an eol in the terminal, but only one is seen as eol by the code, if you manage to scramble them... are you working remotely from win to a unix box?
Last edited on
Is it trying to create an initializer list on the "auto j{i+1};" call for anyone else?
Works correctly once I change the auto to an int. (this is a bad place to use auto, imo.)

Edit: Weird, it seems to only be cpp.sh that doesn't it.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <sstream>
#include <string>
#include <map>
using namespace std;

int main()
{
// istream &in = cin;
   istringstream in( "6    \n"
                     "Alice\n"
                     "Alice\n" 
                     "Bob  \n"
                     "John \n"
                     "Bob  \n" 
                     "Alice\n" );
   string name, output;
   int N;
   map<string,int> M;
   
   in >> N;
   for ( int i = 0; i < N; i++ )
   {
      in >> name;
      M[name]++;
      output += name + ( M[name] > 1 ? to_string( M[name] ) : "" ) + '\n';
   }
   cout << output;
}
Alice
Alice2
Bob
John
Bob2
Alice3

Last edited on
Do you need to get your strings stored in an array of sort or not?
Should it be sorted or not?

Another example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>


std::map<std::string, int> filleMapFromUserInput ();
std::vector<std::string> createVectorFromStdMap (const std::map<std::string, int>&);


int main()
{
    auto names { filleMapFromUserInput() };
    for (const auto& e : createVectorFromStdMap(names)) {
        std::cout << e << '\n';
    }
}


std::map<std::string, int> filleMapFromUserInput ()
{
    std::cout << "How many names will you input? ";
    int howmany;
    std::cin >> howmany;

    std::map<std::string, int> names;
    for (int i {}; i < howmany; ++i) {
        std::cout << "Name? ";
        std::string tmp;
        std::getline (std::cin, tmp);
        ++names[tmp];
    }
    return names;
}


std::vector<std::string> createVectorFromStdMap (const std::map<std::string, int>& mymap)
{
    std::vector<std::string> v (mymap.size());
    std::transform (mymap.begin(),
                    mymap.end(),
                    v.begin(),
                    [](const auto& e) {
                        return std::string(e.first + '_' + std::to_string(e.second));
                    } );
    std::sort(v.begin(), v.end());  // if needed
    return v;
}

Topic archived. No new replies allowed.